WebSocket

WebSocket is a fairly recent communication protocol which provides full-duplex communication over a single TCP connection. This protocol was standardized in 2011. Although this protocol is different from HTTP, there is a certain relation between them.

How are WebSocket and HTTP related?

WebSocket is a protocol to allow two-way communication in full duplex manner. It is a recent development and not all browsers and servers support this protocol. As a result, the client, who initiates the connection with the server, initiates HTTP connection, but with some additional headers. These headers help client and server decide if they can use this new protocol or not. If the server supports this new protocol, the connection is upgraded to WebSocket protocol, else HTTP is continued.

WebSocket
WebSocket Vs HTTP – source: https://blogs.windows.com/buildingapps/2016/03/14/when-to-use-a-http-call-instead-of-a-websocket-or-http-2-0

When should one choose WebSocket?

The main advantage of this protocol is server can push data to the client even without the client asking for it. This protocol is NOT designed around request-response paradigm. Instead, any end can send a message to another end at any point in time. This enables real-time data transfer from server to client (e.g. in a game, a server can constantly push status of the various resources to client and client doesn’t have to request them specifically). You can read this blog for some excellent use cases for WS as well as HTTP

This protocol support both types of connections – plain text (ws://) as well as secure (wss://) – both these URI schemes have been added to WebSocket specification. There is wide support available in various browsers including Chrome, Edge, IE, Firefox, and Safari. So, this gives an opportunity to developers to make use of this protocol in their applications.

Related Links

Related Keywords

HTTP, HTTP/2, Polling, Long Polling, HTTPS, Networking

 

One Reply to “WebSocket”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.