"Demystifying WebSockets: Unveiling the Magic Behind Real-time Communication"
WebSocket
WebSocket is a full-duplex (two-way communication) protocol that is used in the same scenario of client-server communication.
It is a stateful protocol, which means the connection between client and server will keep alive until it is terminated by either client or server, after closing the connection by either of the client and server, the connection is terminated from both the end.
WebSocket does not use the http:// or https:// scheme (because they do not follow the HTTP protocol)
Rather, WebSocket URIs use a new scheme ws: (or wss: for a secure Websocket). The remainder of the URI is the same as an HTTP URI: a host, port, path and any query parameters Example: ws://example.com:8000/ws/chat
How websocket works
The client sends regular HTTP request with an additional header to be requested
The Server gets the HTTP request and notices the request for the Upgrade header. This lets the server know that we are requesting for a websocket connection
If all goes well persistent connection established between client and server
Connection can be closed either by client or server
What is the difference between HTTP and WebSocket?
HTTP | WebSocket |
As both HTTP and WebSocket are employed for application communication | |
HTTP is a unidirectional protocol | 2. WebSocket is a framed and bidirectional protocol |
Stateless | 3. Satefull |
In HTTP, the connection is built at one end, making it a bit sluggish than WebSocket. | 4. In WebSocket, communication occurs at both ends, which makes it a faster protocol |
WebSocket uses a unified TCP connection and needs one party to terminate the connection. Until it happens, the connection remains active. | 5. HTTP needs to build a distinct connection for separate requests. Once the request is completed, the connection breaks automatically. |
How are WebSocket connections established?
The process starts with a WebSocket handshake that involves using a new scheme ws or wss. To understand quickly, you may consider them equivalent to HTTP and secure HTTP (HTTPS) respectively.
Using this scheme, servers and clients are expected to follow the standard WebSocket connection protocol. The WebSocket connection establishment begins with HTTP request upgrading that features a couple of headers such as Connection: Upgrade, Upgrade: WebSocket, Sec-WebSocket- Key, and so on.
Here is how this connection is established:
- The Request: Connection: Upgrade header denotes the WebSocket handshake while the Sec-WebSocket-Key features Base64-encoded random value. This value is arbitrarily generated during every WebSocket handshake. Besides the above, the key header is also a part of this request.
The above-listed headers, when combined, form an HTTP GET request. It will have similar data in it:
GET ws://websocketexample.com:8181/ HTTP/1.1
Host: localhost:8181
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: b6gjhT32u488lpuRwKaOWs=
To clarify, Sec-WebSocket-Version, one can explain the WebSocket protocol version ready to use for the client.
The response: On the success of the previously-sent request, a response similar to the below-mentioned text sequence will be received:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: rG8wsswmHTJ85lJgAE3M5RTmcCE=
Reference :
Wikipedia ( https://en.wikipedia.org/wiki/WebSocket )
Using web sockets ( https://cloud.google.com/run/docs/triggering/websockets )
Web socket protocol (https://www.wallarm.com/what/a-simple-explanation-of-what-a-websocket-is)
Youtube ( https://www.youtube.com/watch?v=xr5BLGxSuFs )
Subscribe to my newsletter
Read articles from Md. Abu Salman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by