Web Socket in Networking

Before diving into the topic, let's first understand what full-duplex communication is.
Full-duplex communication
It refers to a communication system in which data can be transmitted and received simultaneously between two endpoints. In simpler terms, both parties can talk and listen at the same time, without waiting for the other to finish.
Conceptually, full-duplex communication can be thought of as having two separate communication channels, one for each direction.
Advantages and Disadvantages
Applications
Real-Time Messaging Systems: Slack, WhatsApp Web
Live Streams and Broadcasting: YouTube Live, Twitch
IoT Device Communication: Smart homes, industrial automation
Online Multiplayer Games: Data exchange for player actions and server updates
Financial Applications: Real-time trading platforms for bidirectional communication of trades and live market data (Zerodha, Groww)
Telephone Systems / Video Calls
Full-Duplex vs. Other Communication Modes
Now let’s understand about Web Socket.
Web Socket
Web Socket is an application layer protocol that provides full-duplex (two-way) communication between a client (typically a browser) and a server over a single, long-lived connection.
Web Sockets are part of the HTML5 specification and are supported by most modern browsers and servers.
How Web Socket Works
The Web Socket protocol starts with an HTTP request from the client to the server to upgrade the connection. The request includes a header
Upgrade: websocket
andConnect: upgrade
indicating the client's intent to establish a Web Socket connection.The server acknowledges the upgrade (by sending 101 status code) and switches the protocol from HTTP to Web Socket.
After the handshake, the connection remains open, and both the client and server can send messages to each other at any time without additional handshakes.
Messages are transmitted in frames, which can be text, binary, or control frames. This allows efficient communication.
Either the client or the server can terminate the connection by sending a close frame.
Features of Web Socket
Full-Duplex Communication: Both the client and server can send and receive messages independently and simultaneously.
Persistent Connection: The connection stays open, eliminating the need to repeatedly establish new connections.
Low Latency: Data is exchanged with minimal delay, making it suitable for real-time applications.
Lightweight: Compared to HTTP polling or other techniques, Web Socket reduces overhead by avoiding repeated headers and handshakes.
Binary and Text Support: Web Socket can handle both binary and text data, making them versatile.
Cross-Origin Communication: Web Socket supports cross-origin requests, enabling seamless communication across domains.
Advantages and Disadvantages
Use Cases
Chat Applications: Real-time messaging platforms like WhatsApp Web or Slack.
Live Notifications: Push updates for emails, alerts, or social media.
Online Gaming: Multiplayer games requiring low-latency communication.
Real-Time Collaboration: Shared document editing or whiteboarding (e.g., Google Docs).
Financial Applications: Real-time stock prices, currency exchange rates, or market data.
IoT and Device Communication: Real-time updates from IoT devices or sensors.
Comparison with HTTP protocol
Comparison with Other Techniques
Thank you for your time! 😊
Connect with me on LinkedIn
Subscribe to my newsletter
Read articles from Shubham Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
