Close Menu
    Trending
    • Future of Business Analytics in This Evolution of AI | by Advait Dharmadhikari | Jun, 2025
    • You’re Only Three Weeks Away From Reaching International Clients, Partners, and Customers
    • How Brain-Computer Interfaces Are Changing the Game | by Rahul Mishra | Coding Nexus | Jun, 2025
    • How Diverse Leadership Gives You a Big Competitive Advantage
    • Making Sense of Metrics in Recommender Systems | by George Perakis | Jun, 2025
    • AMD Announces New GPUs, Development Platform, Rack Scale Architecture
    • The Hidden Risk That Crashes Startups — Even the Profitable Ones
    • Systematic Hedging Of An Equity Portfolio With Short-Selling Strategies Based On The VIX | by Domenico D’Errico | Jun, 2025
    Finance StarGate
    • Home
    • Artificial Intelligence
    • AI Technology
    • Data Science
    • Machine Learning
    • Finance
    • Passive Income
    Finance StarGate
    Home»Artificial Intelligence»Deep Dive into WebSockets and Their Role in Client-Server Communication | by Clara Chong | Feb, 2025
    Artificial Intelligence

    Deep Dive into WebSockets and Their Role in Client-Server Communication | by Clara Chong | Feb, 2025

    FinanceStarGateBy FinanceStarGateFebruary 3, 2025No Comments6 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    How WebSockets work, its tradeoffs, and tips on how to design an actual time messaging app

    Towards Data Science

    Picture by Kelly from Unsplash

    Actual-time communication is in all places — stay chatbots, knowledge streams, or immediate messaging. WebSockets are a robust enabler of this, however when must you use them? How do they work, and the way do they differ from conventional HTTP requests?

    This text was impressed by a latest system design interview — “design an actual time messaging app” — the place I stumbled by some ideas. Now that I’ve dug deeper, I’d wish to share what I’ve realized so you may keep away from the identical errors.

    On this article, we’ll discover how WebSockets match into the larger image of shopper‑server communication. We’ll talk about what they do properly, the place they fall brief, and — sure — tips on how to design an actual‑time messaging app.

    At its core, client-server communication is the trade of knowledge between two entities: a shopper and a server.

    The shopper requests for knowledge, and the server processes these requests and returns a response. These roles are usually not unique — companies can act as each a shopper and a server concurrently, relying on the context.

    Earlier than diving into the main points of WebSockets, let’s take a step again and discover the larger image of client-server communication strategies.

    1. Brief polling

    Brief polling is the best, most acquainted strategy.

    The shopper repeatedly sends HTTP requests to the server at common intervals (e.g., each few seconds) to verify for brand spanking new knowledge. Every request is impartial and one-directional (shopper → server).

    This methodology is simple to arrange however can waste sources if the server not often has contemporary knowledge. Use it for much less time‑delicate functions the place occasional polling is ample.

    2. Lengthy polling

    Lengthy polling is an enchancment over brief polling, designed to cut back the variety of pointless requests. As an alternative of the server instantly responding to a shopper request, the server retains the connection open till new knowledge is accessible. As soon as the server has knowledge, it sends the response, and the shopper instantly establishes a brand new connection.

    Lengthy polling can also be stateless and one-directional (shopper → server).

    A typical instance is a trip‑hailing app, the place the shopper waits for a match or reserving replace.

    3. Webhooks

    Webhooks flip the script by making the server the initiator. The server sends HTTP POST requests to a client-defined endpoint every time particular occasions happen.

    Every request is impartial and doesn’t depend on a persistent connection. Webhooks are additionally one-directional (server to shopper).

    Webhooks are extensively used for asynchronous notifications, particularly when integrating with third-party companies. For instance, fee techniques use webhooks to inform purchasers when the standing of a transaction adjustments.

    4. Server-Despatched Occasions (SSE)

    SSEs are a native HTTP-based occasion streaming protocol that enables servers to push real-time updates to purchasers over a single, persistent connection.

    SSE works utilizing the EventSource API, making it easy to implement in trendy internet functions. It’s one-directional (server to shopper) and preferrred for conditions the place the shopper solely must obtain updates.

    SSE is well-suited for functions like buying and selling platforms or stay sports activities updates, the place the server pushes knowledge like inventory costs or scores in actual time. The shopper doesn’t have to ship knowledge again to the server in these situations.

    However what about two-way communication?

    All of the strategies above give attention to one‑directional circulation. For true two‑method, actual‑time exchanges, we’d like a special strategy. That’s the place WebSockets shine.

    Let’s dive in.

    WebSockets allow real-time, bidirectional communication, making them good for functions like chat apps, stay notifications, and on-line gaming. Not like the standard HTTP request-response mannequin, WebSockets create a persistent connection, the place each shopper and server can ship messages independently with out ready for a request.

    The connection begins as an everyday HTTP request and is upgraded to a WebSocket connection by a handshake.

    As soon as established, it makes use of a single TCP connection, working on the identical ports as HTTP (80 and 443). Messages despatched over WebSockets are small and light-weight, making them environment friendly for low-latency, high-interactivity use instances.

    WebSocket connections observe a selected URI format: ws:// for normal connections and wss:// for safe, encrypted connections.

    What’s a handshake?

    A handshake is the method of initialising a connection between two techniques. For WebSockets, it begins with an HTTP GET request from the shopper, asking for a protocol improve. This ensures compatibility with HTTP infrastructure earlier than transitioning to a persistent WebSocket connection.

    1. Shopper sends a request, with headers that seem like:
    GET /chat HTTP/1.1
    Host: server.instance.com
    Improve: websocket
    Connection: Improve
    Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
    Origin: http://instance.com
    Sec-WebSocket-Protocol: chat, superchat
    Sec-WebSocket-Model: 13
    • Improve — alerts the request to modify the protocol
    • Sec-WebSocket-Key — Randomly generated, base64 encoded string used for handshake verification
    • Sec-WebSocket-Protocol (elective) — Lists subprotocols the shopper helps, permitting the server to choose one.

    2. Server responds to resquest

    If the server helps WebSockets and agrees to the improve, it responds with a 101 Switching Protocols standing. Instance headers:

    HTTP/1.1 101 Switching Protocols
    Improve: websocket
    Connection: Improve
    Sec-WebSocket-Settle for: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
    Sec-WebSocket-Protocol: chat
    • Sec-WebSocket-Settle for — Base64 encoded hash of the shopper’s Sec-WebSocket-Key and a GUID. This ensures the handshake is safe and legitimate.

    3. Handshake validation

    With the 101 Switching Protocols response, the WebSocket connection is efficiently established and each shopper and server can begin exchanging messages in actual time.

    This connection will stay open until it’s explicitly closed by both celebration.

    If any code apart from 101 is returned, the shopper has to finish the connection and the WebSocket handshake will fail.

    Right here’s a abstract.

    Abstract of WebSockets (drawn by me)

    We’ve talked about how WebSockets allow real-time, bidirectional communication, however that’s nonetheless fairly summary time period. Let’s nail down some actual examples.

    WebSockets are extensively utilized in real-time collaboration instruments and chat functions, corresponding to Excalidraw, Telegram, WhatsApp, Google Docs, Google Maps and the stay chat part throughout a YouTube or TikTok stay stream.

    1. Having a fallback technique if connections are terminated

    WebSockets don’t mechanically get well if the connection is terminated as a result of community points, server crashes, or different failures. The shopper should explicitly detect the disconnection and try to re-establish the connection.

    Lengthy polling is commonly used as a backup whereas a WebSocket connection tries to get reestablished.

    2. Not optimised for streaming audio and video knowledge

    WebSocket messages are designed for sending small, structured messages. To stream massive media knowledge, a expertise like WebRTC is best suited to these situations.

    3. WebSockets are stateful, therefore horizontally scaling is just not trivial

    WebSockets are stateful, which means the server should preserve an energetic connection for each shopper. This makes horizontal scaling extra advanced in comparison with stateless HTTP, the place any server can deal with a shopper request with out sustaining persistent state.

    You’ll want a further layer of pub/sub mechanisms to do that.

    Now let’s see how that is utilized in system design. I’ve lined each the straightforward (unscalable) answer and a horizontally scaled one.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleTop 9 Amazon Textract alternatives for data extraction
    Next Article Ottawa offers taxpayers relief with capital gains tax delay
    FinanceStarGate

    Related Posts

    Artificial Intelligence

    How AI Agents “Talk” to Each Other

    June 14, 2025
    Artificial Intelligence

    Stop Building AI Platforms | Towards Data Science

    June 14, 2025
    Artificial Intelligence

    What If I had AI in 2018: Rent the Runway Fulfillment Center Optimization

    June 14, 2025
    Add A Comment

    Comments are closed.

    Top Posts

    Revolutionizing Automated Visual Inspection – The Role of Robotics in Modern Automated Visual Inspection

    June 3, 2025

    News Bytes 20250526: Biggest AI Training Center?, Big AI Pursues AGI and Beyond, NVIDIA’s Quantum Moves, RISC-V Turns 15

    May 27, 2025

    Building a Scalable Airbnb Pricing and Analytics Pipeline on AWS: A Practical Guide | by Jimmy | May, 2025

    May 17, 2025

    Why Tier 0 Is a Game-Changer for GPU Storage

    February 12, 2025

    Probability, Distributions, and Preprocessing for Data Science | by Mojtaba Ghasemi | Mar, 2025

    March 2, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    Most Popular

    Pinterest CEO Says AI Helped Revenue Grow By 16%

    May 9, 2025

    I want a time machine too. O tempo escorre pelas mãos como grãos… | by Eduardo Portilho | Feb, 2025

    February 18, 2025

    Sentence Transformers, Bi-Encoders And Cross-Encoders | by Shaza Elmorshidy | Mar, 2025

    March 10, 2025
    Our Picks

    How a leading underwriting provider transformed their document review process

    April 24, 2025

    How to Own a Franchise As a Side Hustle

    March 13, 2025

    Jack’s Data Odyssey: The Journey Begins | by Rohit Chaturvedi | Mar, 2025

    March 14, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Data Science
    • Finance
    • Machine Learning
    • Passive Income
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 Financestargate.com All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.