WebSockets Explained

Daniel Patnode
3 min readDec 2, 2020

--

Why WebSockets?

WebSockets — what are those you ask? At the very base level WebSockets are another tool that allows information passing from client to server and vice versa. WebSockets were created with the aim to improve on the limitations traditional HTTP based technologies have in terms of the call and response cycle, by enabling “bidirectional communication between a browser and a web server”(bloggeek.me). With WebSockets information can be passed back and forth simultaneously, after an initial opening of communication between the client and server, while traditional HTTP requests require the client to make requests to the server in order for the server to respond. This lack of limitation allows for faster passing of information and allows for almost real time changes on the browser.

WebSocket Uses

These realtime changes are highly applicable for and useful for:
1) updating social media feeds in real time(twitter, facebook)
2) live tickers (sports, news scrolls, stock market, weather)
3) multimedia chat (video and audio chat)
4) location based applications (GPS, tracking etc.)
5) online education (real time assignments, quizzing, collaborative online whiteboards)
6) collaborative editing/coding( allows for teams to work remotely together online simultaneously)

The ability to have real time, up to date interactions opens up a multitude of possibilities.

Traditional HTTP requests Compared To WebSockets

HTTP vs WebSocket Protocol

In traditional HTTP client server interactions:
1) a client makes a request
2) application server interprets the request
3) application server requests additional info from the database
4) database sends back information to application server
5) application server formats and sends a response to the client

the problem with traditional HTTP client server interactions:
1) Slower, must constantly reestablish connection and renegotiate a connection every time. Big issue if your trying to transfer a lot of data
2) No real time data updates.
3) Cause a huge amount of overhead and introduce latency which has a impact on overall performance.

WebSockets alleviate a lot of these issues!

In WebSocket client server interactions:
1) Communication is Bi-directional- meaning that there is a constant flow of information once a connection is established AND that there is no predefined message pattern.
2) WebSocket is full-duplex while HTTP is half-duplex- meaning that a client can talk to the server AND a server can talk to the client without a prescribed order and be independent of each other. HTTP interactions require the client to send a message to the server first and the server sends a message back. In HTTP client server communication this is the only flow of communication.
3) Utilization of single TCP connections- when a HTTP request is made with WebSockets a new TCP request is initiated and terminated after the response is received. New TCP connections need to be established after a new HTTP response. Client and servers communicate over the same TCP connection for the lifecycle WebSocket connection.

--

--