WebSockets–versus HTTP
I am building a web app (or a mobile app). Can I eliminate HTTP altogether and use WebSockets?
Not completely. To perform a websocket handshake , one needs HTTP. Once the handshake is successful, you can switch to websockets entirely. If your app runs in a browser, you may also need to serve a HTML/JavaScript file(s) for the landing page, which will require HTTP.
What are the advantages and disadvantages of this solution?
Advantages
- Full Duplex, stateful communication (unlike HTTP).
- Real server push (you can easily notify clients) .
- Add a layer on top of WebSockets (e.g. json rpc), and you can outperform HTTP.
Disadvantages
- Bare bones TCP, so your app may require another protocol on top of that. Which could affect performance and reliability. Especially if you want to mix textual content (e.g. json) with binary content (e.g. images). You may end up reinventing the HTTP protocol over websockets.
- Not as many tools as for HTTP
What about Socket.io?
- Socket.io was designed to handle some shortcomings of websockets.
- Addresses Connection dropping issues with WebSockets.
- Old Browser Fallback capability.
Leave a Reply