Getting Started to WebSocket

Anix Sam Saji
3 min readAug 19, 2023
Image by svstudioart

What is a WebSocket?

The WebSocket protocol offers full-duplex message-based communication between the client and server utilizing a single socket, making it suitable for real-time, event-driven communication in the IoT context where data bundles are continuously exchanged among numerous devices. It is applied in the creation of real-time applications, helping to ensure that data is continuously displayed at the client end while being continuously sent back by the backend server. By creating persistent socket connections between a server and client, where the server sends data to connected clients, WebSocket enables two-way low-latency communication between a client and server. WebSocket enables real-time performance and bi-directional communication between a client and the server over a single TCP connection, even if it consumes a lot of power.

WebSocket — Explanation

The protocol enables bi-directional communication, enables client to fetch real-time data from the server. It also enables real-time data push to the server. It supports proxy when used with the ports 80 and 443.

Advantages and disadvantages of WebSocket

The main pros of using WebSocket communication are:

  • Faster connection.
  • Request/response streaming in real-time
  • High efficiency and low latency
  • End-to-end support for a duplex connection

The main drawbacks of using WebSocket communication are:

  • A fully HTML5-compliant web browser is required.
  • AJAX-like success mechanisms are not available in WebSockets.
  • WebSockets, unlike HTTP, do not provide intermediary/edge caching.
  • It is impossible to employ friendly HTTP statuses, bodies, and other elements to create even a simple protocol of your own.
  • HTTP is significantly easier to develop if your application doesn’t take a lot of dynamic interaction.

Difference between HTTP and WebSocket

Source : wallarm.com

While reading the comparative table above, some of you might be unclear as to what half duplex and full duplex mean. Here is a brief explanation of what it entails.

What is Duplex?

A duplex communication system is a point-to-point system which consists of two or more connected nodes(devices) that can communicate with one another in both direction.

Half duplex

Since duplex is point-to-point communication between two devices, half duplex means that one node(device) can only transmit or receive at a time. Once one node has finished transmitting its data, another node can start transmitting data. If multiple nodes try to communicate at the same time, a conflict will occur on the network, resulting in transmission errors or data loss.

Full duplex

On the other hand full-duplex is the communication between two nodes where the nodes communicating to each other are able to send and receive data at the same time. In these cases, there is no danger of a conflict, and therefore, the transfer of data for any given communication is completed more quickly.

When to use WebSocket

WebSocket communication can be used only if an application demands real-time communication or need to fetch data continuously from the server, it can also be used in application which need to collect data continuously without disrupting the UI. Examples of application which can be used with WebSocket are:

Gaming apps: Gaming apps require more data to be transmitted and received without the UI getting disrupted.

Chat application: Chat application require real-time communication between two users, also the connection should be active once the users start chatting.

IoT applications: IoT application require continuous data to be transmitted and received real-time from the sensors and devices to the server for the data processing.

--

--