Websockets Simplified

Why use and How it works?

ยท

3 min read

Websockets Simplified

Web socket is very similar to a two-way street.

338215550-Two-Way-Street.jpg

First let's understand why we need Websockets ?

We use websockets when we desire real time communication between a web server and a client and need two-way communication. Applications like chat apps, real time data analytics, social media etc. uses websockets for real time data transfer.

So what is a Web socket ?

WebSocket is a protocol providing full-duplex communication channels over a single TCP connection.

How this protocol is different from traditional HTTP ?

In the Traditional HTTP protocol, we have

  • Client makes request.
  • Application Server interprets request.
  • Application Server requests additional information from database if necessary.
  • Database sends info back to Application Server.
  • Application Server formats and sends response to client.

So what's wrong with this ?

  • Can be slow due to HTTP overhead : We are constantly reestablishing a connection and must negotiate the connection every single time, this multiple responses introduce latency and obviously going to have an impact on performance.

Here web socket comes into picture

  • Client opens connection with server known as socket handshake.
  • Client and server send data to each other as data becomes available on either side.
  • The overhead is minimal and instead of hundreds of fights and requests and response headers the server sent responses only once the data has been exchanged.
  • WebSocket is a Bi-directional protocol and HTTP is a unidirectional protocol, which means that the request is always initiated by client server processes the requests and which forms a response and then the client consumed it. There are no predefined message patterns such as request response, so either a client or server can send a message to the other party.
  • Websocket is full duplex and HTTP is a half duplex which means that HTTP allows request message to go from client to server and then server sends a response message to the client at a given time either client is talking to server or server is talking to client with websockets either client or the server can be talking to each other at the same time and be independent of each other.
  • Websockets uses single TCP connection as opposed to HTTP, so in typical HTTP protocol a new TCP connection is initiated for an HTTP request and terminated after the response is received and a new TCP connection is need to be established when another HTTP request/response. In WebSockets the HTTP connection is upgraded using sender HTTP upgrade mechanism and client and server communicate over the same TCP connection for the lifecycle of webSocket connection.

How to use WebSockets

To use WebSockets in Back-end applications, there are libraries exist for many languages including Node, Ruby, Python PHP etc. Socket.io is a popular node.js library which comes with client and server implementation of the protocol. It abstracts websocket communication.

Did you find this article valuable?

Support Mohit Kushwaha by becoming a sponsor. Any amount is appreciated!

ย