What are WebSockets ?

Ashutosh Mishra
3 min readApr 30, 2021

What is a socket?

A socket is one endpoint of a two way communication link between a client and a server which can be used to establish a real-time, bidirectional and event-based communication. In simpler terms a socket can be understood with the analogy of a phone. User A has to initiate communication by dialing to User B. Once the call is received a bidirectional channel is established and real time communication becomes possible.

Photo by Jim Reardan on Unsplash

What is a WebSocket?

WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.The WebSocket protocol enables interaction between a web browser (or other client application) and a web server facilitating real-time data transfer from and to the server.

How are WebSockets different from HTTP?

In a typical HTTP (http or https) connection a request is sent to and endpoint which returns a response and then the connection closes. This is repeated anytime a communication has to made via HTTP.

In WebSockets (ws or wss) there is a bidirectional communication between WebSockets the sender and receiver both can send data. Whenever a communication is made between client and server a handshake is performed and the connection is opened.Once the connection is open the communication happens in the form of bidirectional messaging. Once the connection is opened it exists until one of the sides(client or server) decides to close the connection. The the connection is terminated from both the ends.

Uses of WebSockets:-

  1. Real time applications (ex chat apps, trading apps etc)
  2. Gaming applications

How can you implement sockets in your applications?

Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of:

1. A Node.js server

2. A js client library for browsers

Features

Here are the features provided by Socket.IO over plain WebSockets:

· reliability (fallback to HTTP long-polling in case the WebSocket connection cannot be established)

· automatic reconnection

· packet buffering

· acknowledgements

· broadcasting to all clients or to a subset of clients (what we call “Room”)

· multiplexing (what we call “Namespace”)

For more read the docs

--

--