As We know socket connection is faster than REST API, is there any problem if I create an entire application using socket io for any request instead of REST? is there any drawback if I do that? Thank you
As with anything, there are pro’s and con’s to each approach, that only come into play depending on what your application is, and its requirements.
Web sockets aren’t always faster. If your application needs to get data from your server, a web socket requires the entire connection to be setup before you can get the data, for single calls this is dramatically slower than a normal HTTP request. This becomes less of a problem if you use the same connection over time, which is where you could see small performance gains.
However, there’s also the situation where you use a GET HTTP request for something you already got in the past, due to the nature of REST API’s, a GET is stateless, and thus could be cached by the browser and thus is nearly instant compared to a websocket call.
Finally, speed isn’t everything. Sure your requests comeback milliseconds faster, but if your app needs to scale, websockets become more of a headache as you increased you own complexity dramatically with a websocket only setup, compared to the near universal support for HTTP requests that can be spread out any multitude of ways. Or if you aren’t leveraging the key feature of websockets, where a server can “tell” the client about something, you increased you own performance overhead for a minimal improvement in performance.
I’d only go with a websocket only setup if you plan on leveraging the key features of websockets that aren’t available with HTTP requests. HTTP/REST API’s are too universal, too well documented, too well supported, and still run fast enough when done correctly to not use.
If you need a single connection kept open all the time to transfer data back and forth, Websockets are great. But the tradeoff is that it’s generally a bit more complicated and needs a lot more setup, and things like handling interrupted connections is a bit painful. Websockets are stateful, HTTP is not (the comparison is between WS and HTTP, REST is an architectural style). If you need real-time updates between two connected machines WS’ are pretty good, if you don’t then can take the much easier route of using the request-response model of HTTP.
By analogy, it’s like comparing telephone and sending letters as ways of communicating. The telephone is a lot more complex to set up, but enables people to talk in real-time. Letters are a lot simpler, but to have a conversation need to send one, them the recipient needs to send a reply and so on.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.