
What are WebSockets?
WebSockets function as a communication protocol, offering full-duplex communication channels over a single, persistent TCP (Transmission Control Protocol) connection. They designed this to overcome some limitations inherent in traditional HTTP (Hypertext Transfer Protocol) communication, which primarily relies on a request-response model.
The WebSocket protocol initiates with a handshake between the client and server utilizing the HTTP protocol. Upon completing the handshake, the connection transitions to the WebSocket protocol, enabling bidirectional communication. WebSockets find frequent application in web-based functionalities, facilitating real-time features like live chat, notifications, collaborative editing, financial trading platforms, and online gaming.
Some of the key features of the WebSockets include:
1. Full-Duplex Communication:
WebSockets allow for two-way communication, meaning that both the client and server can send messages to each other independently over the same connection. This is in contrast to traditional HTTP, where the client sends a request and waits for the server to respond.
2. Bidirectional Communication:
WebSockets facilitate bidirectional communication, enabling initiation of communication by either the client or the server. This proves advantageous in situations demanding real-time updates or notifications.
3. Protocol Overhead:
The WebSocket protocol incurs minimal overhead in comparison to HTTP, offering advantages in scenarios demanding frequent communication. This is particularly beneficial for applications requiring real-time interaction, including online gaming, financial applications, and chat applications.
4. Low-Latency:
Because WebSockets use a single, long-lived connection, they can reduce latency compared to multiple short-lived connections used in traditional HTTP. This makes them suitable for real-time applications where low latency is crucial.
What are WebSockets used for?
WebSockets enable real-time bidirectional communication between a client (typically a web browser) and a server through a single, persistent TCP connection. This technology proves especially effective in scenarios where low-latency, high-throughput, and real-time updates are essential. Consequently, here are some common use cases for WebSockets:
1. Real-Time Web Applications:
WebSockets find extensive use in developing real-time web applications, including chat applications, collaborative document editing, online gaming, and live streaming. The bidirectional nature of WebSockets allows instant communication between clients and servers.
2. Online Gaming:
Online gaming frequently relies on WebSockets to enable real-time communication between players and the game server.This is crucial for multiplayer games where quick responses and synchronization are essential for a seamless gaming experience.
3. Notifications and Alerts:
WebSockets serve as a means to deliver instant notifications and alerts to users. This can include push notifications for updates, system alerts, or any event-triggered communication that requires immediate attention.
4. IOT(Internet of Things):
In IoT applications, WebSockets serve for bidirectional communication between IoT devices and the server. This allows for real-time monitoring, control, and updates in scenarios like smart homes, industrial automation, and connected vehicles.
Difference Between HTTP And WebSocket.
1. Communication Style:
HTTP:
Request-Response: HTTP is primarily based on the request-response model. A client sends a request to a server, and the server responds with the requested data.
Stateless: Each request from the client is independent, and the server does not maintain any state between requests.
WebSocket:
Full-Duplex: WebSocket provide full-duplex communication, allowing both the client and server to send messages independently over the same, long-lived connection.
Bidirectional: Either the client or the server can initiate communication at any time.


2. Security (Encryption):
HTTP:
Encryption: HTTP encrypts the communication between the client and the server, providing a secure and private channel. It uses TLS (Transport Layer Security) or its predecessor, SSL (Secure Sockets Layer), to encrypt the data.
WebSocket:
Encryption: WebSockets can be used over a secure connection (WSS – WebSocket Secure) to encrypt the data, providing a secure channel similar to HTTP.
3. Use Cases:
HTTP:
Web Pages: HTTP is used for loading secure web pages, where sensitive information such as login credentials or personal data is transmitted.
Traditional Web Applications: It is suitable for traditional web applications that follow the request-response paradigm.
WebSocket:
Real-Time Applications: WebSocket are ideal for real-time applications where low latency and bidirectional communication are essential, such as chat applications, online gaming, and live updates.


4. Connection Handling:
HTTP:
Short-Lived Connections: In HTTP, each interaction typically involves a short-lived connection. The client establishes a connection for a request, and the connection is closed after the response is received.
WebSocket:
Long-Lived Connections: WebSocket keep the connection open for an extended period, allowing for efficient communication without the need to repeatedly establish connections.
Drawbacks of WebSockets.

1. Browser Support:
Using WebSockets in your application might be a problem for people using older web browsers because not all of them support this technology. This limitation could affect how many people can access your application. To make sure everyone can use it, you might need to add extra features or “fallback mechanisms” for those older browsers. These fallback mechanisms are like backup plans that allow the application to work differently on browsers that don’t support WebSockets. It’s important to consider this when building your application to ensure a better experience for all users, regardless of the browser they are using.
2. Scalability:
Scalability becomes a concern with WebSockets because they keep a continuous connection between clients and servers, potentially taxing server resources when handling numerous connections simultaneously. To address this, it’s crucial to implement effective load balancing and resource management strategies. Ensuring scalability involves distributing the incoming connections efficiently across multiple servers and optimizing resource usage.


3. Stateful Nature:
WebSockets are different from regular HTTP because they are “stateful,” meaning the server has to remember the connection details for each user. This remembering of connections can use more computer memory, and as more people connect, it might become challenging to handle all those connections. It’s like having a long conversation where the server has to remember what it talked about with each person. To tackle this, it’s important to carefully manage resources and come up with clever ways to handle lots of connections without slowing things down. Balancing this memory use is crucial, especially when dealing with many users at the same time.
4. Proxy and Firewall Limitation:
WebSocket connections may face challenges from certain proxy servers and firewalls, which might block or disrupt the connection. In places like company networks or restricted environments, this interference can lead to problems with connecting to the WebSocket. It’s like trying to make a phone call, but there’s something in between that doesn’t let the conversation happen smoothly. Dealing with these limitations involves adjusting the network settings or finding alternative solutions to ensure that WebSocket connections can pass through without disruptions.

Conclusion.
In summary, WebSockets offer a powerful solution for creating real-time features in various applications, spanning web, mobile, and desktop platforms. However, they aren’t a one-size-fits-all solution. Think of WebSockets as a valuable tool in a broader toolkit for developing applications that require quick communication. While WebSockets are great, they’re not the only option. You can enhance your real-time application by combining WebSockets with other methods like Server-Sent Events (SSE) or long polling. This combination allows you to build an even more robust and scalable real-time experience. The challenge, though, is that using WebSockets might be tricky if you’re not an expert in building real-time systems. It’s essential to weigh the benefits against the potential challenges and choose the right approach based on your specific application needs.
References
https://www.playframework.com/documentation/2.9.x/ScalaWebSockets#:~:text=Asynchronous%20HTTP%20programming-,WebSockets,the%20server%20and%20the%20client.
https://doc.akka.io/docs/akka-http/current/server-side/websocket-support.html