What is websocket ?

What is websocket ?

Half-duplex Communication

Traditionally, in a half-duplex system, communication between two devices is possible, but only one device can transmit data at a time while the other listens, similar to how a two-way radio (like a walkie-talkie) works. The HTTPS protocol operates as a half-duplex protocol.

half-duplex.png

HTTPS (HyperText Transfer Protocol Secure) operates similarly in the sense that the client (browser) and server can communicate back and forth, but the requests and responses follow a pattern where one party sends data, and the other responds. For example:

  • A browser (client) sends a request to the server.
  • The server processes the request and sends back a response.
  • The client processes the response and can send further requests based on it.

While HTTPS is not exactly half-duplex, the analogy holds in that there is a structured, turn-based exchange of messages between client and server, ensuring secure communication without both sides talking over each other.

Full-duplex Communication

In full-duplex communication, data can be sent and received simultaneously. It's like having a phone call where both people can talk and listen at the same time without interruption. This is in contrast to half-duplex, where only one side can communicate at a time.

full-duplex.png

Unlike https, WebSockets is a communication protocol that enables full-duplex communication between a client and a server over a single, long-lived connection. WebSockets allow both the client and server to send messages at any time.

This makes WebSockets ideal for applications that require real-time updates, such as chat applications, live notifications, and online gaming.

whatapp-websocket-simple.png

Key Features of WebSockets

  • Full-Duplex Communication: Both client and server can send and receive data independently and simultaneously.

  • Low Latency: Since the connection remains open, there's minimal delay in communication, making it perfect for real-time applications.

  • Efficiency: Reduced overhead as there's no need to repeatedly establish new connections.

  • Scalability: Suitable for applications that require managing numerous simultaneous connections.

Use Cases

  • Chat Applications: Platforms like WhatsApp and Slack use WebSockets to enable real-time messaging.

  • Online Gaming: Real-time interaction between players, ensuring a seamless gaming experience.

  • Live Updates: Financial trading platforms, live sports updates, and news feeds.

  • Collaborative Tools: Google Docs, Confluence Wiki and similar tools use WebSockets for real-time collaboration.

Takeaways

WebSockets are a powerful tool for enabling real-time, low-latency communication between clients and servers. Their full-duplex nature and efficiency make them ideal for a wide range of modern applications. As technology continues to evolve, the role of WebSockets in creating seamless and interactive experiences will only grow.