Understanding the purpose of WebSocket

LOKESH KUMAR
2 min readApr 13, 2021

Websocket is a technology similar to the rest api which is used for client-server communication, but in websocket two way communication happens ie Server can also send messages to the client without getting any request from the client. Normally in Rest Api, Server can only send a response if the client has made any request. Got a bit confused here is an example

Let’s consider we are building a chat system

If we implement it using Rest Api, normally this would happen

Client Server communication in Rest Api Strucutre

Clients will make api calls to the server in a certain interval and ask for any new message. If any message are there it sends back it to the client

And the same example with the help of websocket happens like this

Server Sent Events with the example of multiple client

Initially the client will establish connection with the server by doing the authentication. Once the connection is established, the connection will stay open. Now the client does not have to repeatedly ask the server for any new messages. As the new messages arrives Server immediately sends them to the client. This is also known as SSE (Server Send Events). In Websocket clients can also send and receive data simultaneously

--

--