What is WebSockets?
Traditional web is built around the HTTP request and response mechanism, where the client sends a request to the server and the server responds with the requested data. This happens in a separate connection for each request and response, which means HTTP is not well-suited for real-time applications that require continuous, bidirectional communication. To solve this problem, we use WebSockets.
- WebSocket provides bidirectional communication between the client and server. In other words, it provides a full-duplex communication channel over a single TCP connection and allows data transfer in both directions.
- Once a WebSocket connection is established between the client and server, it remains open until either the server or client terminates it. So this will help in continuous, real-time communication between the client and the server without the need for frequently re-establishing the connection.
- WebSocket and HTTP protocols have differences, but both rely on the TCP protocol at the 4th layer (transport layer) of the OSI model, and they exist at the 7th layer (application layer).
- WebSocket provides lightweight communication, which reduces the overhead associated with HTTP.
- By maintaining a persistent single TCP socket connection between the client and server, WebSocket provides a low-latency connection for responsive communication. It is a crucial component in developing real-time applications like multiplayer games or any application that requires quick, reliable, and bidirectional data transfer.
How does WebSocket works?
- To establish a WebSocket connection, the client first initiates a handshake with the server by sending an HTTP request with an "Upgrade" header to upgrade the connection to a WebSocket connection. Note: The client opens a WebSocket connection by calling the WebSocket constructor, which uses the URL schemas "ws:" or "wss:" (Just like "http:" or "https:" for HTTP).
- Once the client's request is received by the server, the server accepts it by sending a response with a "101 Switching Protocols" status code and a header indicating the use of WebSockets.
- With the established WebSocket connection, both the client and server can send messages to each other without the need for a new request-response cycle.
- The messages are sent in a binary format using the send() method on the connection object. In the latest specification, the WebSocket connection supports sending both string and binary messages using Blob or ArrayBuffer objects.
- The WebSocket connection remains open until either the client or server closes it.
Key Features of WebSockets
- Two-Way Data Exchange: WebSockets are essential in reducing network traffic by enabling the simultaneous transfer of data in both directions using a single connection.
- HTTP Compatibility: WebSockets are highly compatible with previous versions of HTTP connections, enabling us to switch between HTTP and WebSockets.
- Publish-Subscribe Event Pattern: WebSockets provide an efficient data transfer model by establishing communication channels. This allows messages to be sent to and from a server, as well as receiving event-driven responses without having to continuously poll the server.
Real-life applications of WebSockets
WebSockets are commonly used in various types of applications that require real-time, bidirectional communication between the client and server. Here are some examples:
- Real-time messaging: Web Sockets are essential in real-time communication like instant chat applications. They provide fast data transfer, making it possible to build complex features like encrypted messages, typing indicators and more.
- Multiplayer gaming: Web Sockets allow for low-latency and real-time synchronisation of game states between players. This results in interactive gaming experiences across multiple devices.
- Live online maps: Real-time location data is crucial in building live online maps. Web Sockets allow for efficient transfer of this data, enabling the routing and navigation of moving assets in real-time.
- Live updates: Web Sockets are valuable in providing real-time updates for various platforms like live election results, live score updates, etc.
Conclusion
Web Sockets have genuinely revolutionised web development. Using their stateful and bi-directional nature, they are beneficial in systems that require real-time updates or continuous data streams. However, if we only need to fetch only once, then a simple HTTP request would be considered compared to Web Sockets.
Thanks Suyash Namdeo for his contribution in creating the first version of this content. Please write in the message below if you find anything incorrect or want to share more insight. Enjoy learning. Enjoy algorithms!

