What is a WebSocket?

July 15, 2023

Written by Andalib Kibria and Kamal Hossain

cover-image

Introduction

WebSocket is a communication protocol to connect with servers of any website or mobile application. A communication protocol is a system of rules that allow two or more entities to transmit information through various measures. These are a set of governed rules, syntax, semantics, and synchronization of communication, all of which can be implemented by the combination of both hardware and/or software. Some examples of WebSockets are real-time chat messaging, online/offline status, activity updates and notifications.

To further elaborate on this topic, WebSockets maintain an open connection between the client and the server. Using a PING/PONG mechanism, the server sends a PING packet to the client, then the client’s browser sends a PONG packet back to the server. If the server does not receive the PONG packet within a specified time frame, then it will invoke another set of protocols or close the connection. This is a better approach than simply relaying the packet without waiting for the response.

image-pingpong

Benefits

Like we mentioned above, WebSockets keep the connection active between the server and the client. Below is a list of benefits from this use case scenario:

  • Real time communication. Both the client and server can communicate without any interruption at the same time, such as a full duplex communication.
  • New data can be pushed to all the connected clients from the server, as soon as the data has arrived. This is called server push, which is useful because the client does not have to constantly check for data updates.
  • Due to the low latency, the delay of each data transfer between client and server is quicker.
  • WebSockets can be secured with TLS/SSL (Transport Layer Security / Secure Sockets Layer) encryption. Sensitive data can also be easily transferred.

HTTP & WebSockets

HTTP (Hypertext Transfer Protocol) is a communication protocol used in browsers, much like a WebSocket. There are some key differences between them as discussed below:

image-comparison_table

Scalability

The WebSocket protocol has been in existence since 2008. With support and new improvements, this technology is still competitive in 2023. Scalability is a key factor when developing an application. Typically, REST (Representational State Transfer) APIs (Application Programming Interface) are used because RESTful HTTP are stateless and can be easily scaled.

Since WebSockets are stateful, meaning it has perfect memory or knowledge of the previous requests, this approach becomes memory intensive, and the server bears a considerable burden for the functionality of the application. Therefore, adding more powerful servers may not be a favorable practice. One workaround is to create additional components in the process, such as a load balancer. A load balancer evenly redirects traffic to available servers if the previous server is overloaded or defective. This is when request timeouts are configured, such that if the request to the server is not answered within 5 seconds, then another request is made to a different server. Once more, deploying multiple servers may not be a favorable practice, but if the servers are performing numerous requests from numerous different applications, then the cost to benefit ratio becomes satisfactory.

image-load_balancer

Conclusion

We used this technology on the ShouldertoCryOn (web)app. The messaging segment in the application uses a WebSocket to seamlessly allow users to chat with one another. Furthermore, notifications and alerts also use a WebSocket. The WebSocket enables the communication protocol to alert the user, instead of refreshing the page or reloading the app, thereby creating a better User Experience (UX). If you have any confusion about using this technology for your next project, feel free to contact us.

Share this at