Facebook Instagram Twitter Vimeo Youtube
Sign in
  • Home
  • About
  • Team
  • Buy now!
Sign in
Welcome!Log into your account
Forgot your password?
Privacy Policy
Password recovery
Recover your password
Search
Logo
Sign in
Welcome! Log into your account
Forgot your password? Get help
Privacy Policy
Password recovery
Recover your password
A password will be e-mailed to you.
Thursday, August 28, 2025
Sign in / Join
  • Contact Us
  • Our Team
Facebook
Instagram
Twitter
Vimeo
Youtube
Logo
  • Home
  • News
    • News

      New Cybercrime Technique “Ghost-Tapping” Targets Mobile Wallets by Paige Henley

      26 August 2025
      News

      CISA Issues 10 Security Advisories for Industrial Control Systems by Husain Parvez

      26 August 2025
      News

      ClickFix Attack Surges as Hackers Trick Users Into Running Malware by Husain Parvez

      26 August 2025
      News

      Healthplex Pays $2 Million to Settle NYDFS Cybersecurity Violations After Phishing Attack by Husain Parvez

      26 August 2025
      News

      CIRO Shuts Down Systems After Cybersecurity Threat: Personal Data Exposed by Husain Parvez

      26 August 2025
  • Data Modelling & AI
    • AllBig dataBusiness AnalyticsData ScienceData Structure & AlgorithmDatabasesVector DatabaseDeep LearningEthical HackingGenerative AIMachine Learning
      Big data

      Stop Your AI Assistant from Writing Outdated Code with Milvus SDK Code Helper

      26 August 2025
      Big data

      A Practical Guide for Choosing the Right Vector Database for Your AI Applications

      26 August 2025
      Big data

      Why I’m Against Claude Code’s Grep-Only Retrieval? It Just Burns Too Many Tokens

      26 August 2025
      Big data

      Hands-On with VDBBench: Benchmarking Vector Databases for POCs That Match Production

      16 August 2025
    • Big data
    • Business Analytics
    • Databases
    • Data Structure & Algorithm
    • Data Science
    • Deep Learning
    • Ethical Hacking
    • Generative AI
    • Machine Learning
    • Security & Testing
  • Mobile
    • AllAndroidIOS
      Android

      Google Photos is getting a fun new sticker feature, but it’s not on Android

      26 August 2025
      Android

      I tried popular AI editing apps on Android — only one earned a spot on my phone

      26 August 2025
      Android

      Google is testing a new Discover feed feature for all you multilinguists out there

      26 August 2025
      Android

      Nothing seemingly caught passing off stock photos as Phone 3 camera samples

      26 August 2025
    • Android
    • IOS
  • Languages
    • AllAjaxAngularDynamic ProgrammingGolangJavaJavascriptPhpPythonReactVue
      Languages

      Working with Titles and Heading – Python docx Module

      25 June 2025
      Languages

      Creating a Receipt Calculator using Python

      25 June 2025
      Languages

      One Liner for Python if-elif-else Statements

      25 June 2025
      Languages

      Add Years to datetime Object in Python

      25 June 2025
    • Java
    • Python
  • Guest Blogs
  • Discussion
  • Our Team
HomeData Modelling & AIBig dataWhat are WebSockets and Why are they Used?
Big dataGuest Blogs

What are WebSockets and Why are they Used?

Algomaster
By Algomaster
26 July 2025
0
0
Share
Facebook
Twitter
Pinterest
WhatsApp

    What are WebSockets and Why are they Used?

    #28 System Design – WebSockets

    Ashish Pratap Singh's avatar

    Ashish Pratap Singh
    Aug 28, 2024

    Websockets are a communication protocol used to build real-time features by establishing a two-way connection between a client and a server.

    Imagine an online multiplayer game where the leaderboard updates instantly as players score points, showing real-time rankings of all players.

    This instantaneous update feels seamless and keeps you engaged, but how does it actually work?

    The magic behind this real-time experience is often powered by WebSockets.

    WebSockets enable full-duplex, bidirectional communication between a client (typically a web browser) and a server over a single TCP connection.

    Unlike the traditional HTTP protocol, where the client sends a request to the server and waits for a response, WebSockets allow both the client and server to send messages to each other independently and continuously after the connection is established.

    In this article, we will explore how websockets work, why/where are they used, how it compares with other communication methods, challenges and considerations and how to implement them in code.


    If you’re finding this newsletter valuable and want to deepen your learning, consider becoming a paid subscriber.

    As a paid subscriber, you’ll receive an exclusive deep-dive article every week, access to a structured System Design Resource (100+ topics and interview questions), and other premium perks.

    Unlock Full Access


    1. How do WebSockets work?

    The WebSocket connection starts with a standard HTTP request from the client to the server.

    However, instead of completing the request and closing the connection, the server responds with an HTTP 101 status code, indicating that the protocol is switching to WebSockets.

    After this handshake, a WebSocket connection is established, and both the client and server can send messages to each other over the open connection.

    Step-by-Step Process:

    1. Handshake

    The client initiates a connection request using a standard HTTP GET request with an “Upgrade” header set to “websocket”.

    If the server supports WebSockets and accepts the request, it responds with a special 101 status code, indicating that the protocol will be changed to WebSocket.

    2. Connection

    Once the handshake is complete, the WebSocket connection is established. This connection remains open until explicitly closed by either the client or the server.

    3. Data Transfer

    Both the client and server can now send and receive messages in real-time.

    These messages are sent in small packets called frames, and carry minimal overhead compared to traditional HTTP requests.

    4. Closure

    The connection can be closed at any time by either the client or server, typically with a “close” frame indicating the reason for closure.


    2. Why are WebSockets used?

    WebSockets offer several advantages that make them ideal for certain types of applications:

    • Real-time Updates: WebSockets enable instant data transmission, making them perfect for applications that require real-time updates, like live chat, gaming, or financial trading platforms.

    • Reduced Latency: Since the connection is persistent, there’s no need to establish a new connection for each message, significantly reducing latency.

    • Efficient Resource Usage: WebSockets are more efficient than traditional polling techniques, as they don’t require the client to continuously ask the server for updates.

    • Bidirectional Communication: Both the client and server can initiate communication, allowing for more dynamic and interactive applications.

    • Lower Overhead: After the initial handshake, WebSocket frames have a small header (as little as 2 bytes), reducing the amount of data transferred.

    Share


    3. WebSockets vs. HTTP, Polling, and Long-Polling

    To understand the advantages of WebSockets, it’s helpful to compare them with other communication methods:

    HTTP:

    • Request-Response Model: In HTTP, the client sends a request, and the server responds, closing the connection afterward. This model is stateless and not suitable for real-time communication.

    • Latency: Since each interaction requires a new request, HTTP has higher latency compared to WebSockets.

    Polling:

    • Repeated Requests: The client repeatedly sends requests to the server at fixed intervals to check for updates. While this can simulate real-time updates, it is inefficient, as many requests will return no new data.

    • Latency: Polling introduces delays because updates are only checked periodically.

    Long-Polling:

    • Persistent Connection: In long-polling, the client sends a request, and the server holds the connection open until it has data to send. Once data is sent or a timeout occurs, the connection closes, and the client immediately sends a new request.

    • Latency: This approach reduces the frequency of requests but still suffers from higher latency compared to WebSockets since it requires the client to repeatedly send new HTTP requests after each previous request is completed.

    • Resource Usage: Long-polling can lead to resource exhaustion on the server as it must manage many open connections and handle frequent reconnections.

    WebSockets:

    • Bi-Directional: Unlike HTTP, polling, and long-polling, WebSockets allow for two-way communication.

    • Low Latency: Because the connection remains open, data can be sent and received with minimal delay.

    • Efficiency: WebSockets are more efficient in terms of resource usage and bandwidth.


    4. Challenges and Considerations

    While WebSockets offer numerous benefits, there are some challenges to consider:

    • Proxy Servers: Some proxy servers don’t support WebSocket connections and certain firewalls may block them.

    • Scalability Concerns: Managing a large number of WebSocket connections can be challenging. Consider using load balancers and distributed WebSocket servers to handle large-scale deployments.

    • Fallback Mechanism: Not all clients or networks support WebSockets, which can lead to connectivity issues. Implement fallback mechanisms like long-polling for clients that cannot establish WebSocket connections.

    • Network Reliability: WebSockets rely on a persistent connection, which can be disrupted by network issues. Implementing reconnection strategies and heartbeat mechanisms (regular ping/pong messages) can help maintain the connection’s stability and detect when a connection has been lost.

    • Security: WebSockets are vulnerable to attacks such as Cross-Site WebSocket Hijacking and Distributed Denial of Service (DDoS) attacks. Implement secure WebSocket connections (wss://), authenticate users, and validate input to protect against common vulnerabilities.


    5. Where are WebSockets Used?

    1. Real-Time Collaboration Tools

    Applications like Google Docs may use WebSockets to enable multiple users to edit a document simultaneously. Changes made by one user are instantly reflected for all others, creating a seamless collaborative experience.

    2. Real-Time Chat Applications

    One of the most popular uses of WebSockets is in real-time chat applications.

    Messaging platforms like Slack use WebSockets to deliver messages instantly. This allows for real-time conversations and immediate message delivery notifications.

    3. Live Notifications

    Social media platforms use WebSockets to push real-time notifications to users when they receive a new message, like, or comment.

    Instead of the client constantly checking for new notifications, the server can push updates to the client as soon as they occur.

    4. Multiplayer Online Games

    In online multiplayer games, low latency is crucial for a seamless gaming experience.

    WebSockets provide the necessary real-time communication between the game server and players, ensuring that all players see the same game state simultaneously.

    5. Financial Market Data Feeds

    WebSockets are widely used in financial applications to stream real-time market data, such as stock prices, forex rates, and cryptocurrency values.

    6. IoT (Internet of Things) Applications

    In IoT applications, devices often need to communicate with a server in real time.

    WebSockets provide a lightweight and efficient communication channel for sending sensor data, receiving commands, and synchronizing device states.

    7. Live Streaming and Broadcasting

    While the actual video streaming typically uses other protocols, WebSockets can be used for real-time chat, viewer counts, and other interactive features during live broadcasts.


    6. Implementing WebSockets

    To demonstrate how WebSockets work, let’s look at a simple implementation using Node.js on the server side and JavaScript on the client side.

    Server-Side (Node.js):

    Client-Side (JavaScript):


    7. Conclusion

    WebSockets have revolutionized how we build real-time web applications, providing an efficient, low-latency communication channel that supports full-duplex data exchange.

    From chat applications and live notifications to online gaming and IoT devices, WebSockets enable the creation of responsive and engaging user experiences.

    However, with great power comes great responsibility. Implementing WebSockets requires careful consideration of scalability, security, and resource management to ensure your application performs well under all conditions.


    Thank you for reading!

    If you found it valuable, hit a like ❤️ and consider subscribing for more such content every week.

    If you have any questions or suggestions, leave a comment.

    This post is public so feel free to share it.

    Share


    P.S. If you’re finding this newsletter helpful and want to get even more value, consider becoming a paid subscriber.

    As a paid subscriber, you’ll receive an exclusive deep dive every week, access to a comprehensive system design learning resource , and other premium perks.

    Get full access to AlgoMaster

    There are group discounts, gift options, and referral bonuses available.


    Checkout my Youtube channel for more in-depth content.

    Follow me on LinkedIn, X and Medium to stay updated.

    Checkout my GitHub repositories for free interview preparation resources.

    I hope you have a lovely day!

    See you soon,
    Ashish

    Share
    Facebook
    Twitter
    Pinterest
    WhatsApp
      Previous article
      How to see blocked phone numbers on your Android phone
      Next article
      This phone redefines affordability and raises the bar for value with its new $110 price tag
      Algomaster
      Algomasterhttps://blog.algomaster.io
      RELATED ARTICLES
      Guest Blogs

      Interview With Rony Cohen – Co-Founder and Head of Business Development at FloLIVE by Shauli Zacks

      26 August 2025
      Guest Blogs

      Why CloudThrill Believes the Future of AI Lies in Open Source and Flexibility by Petar Vojinovic

      26 August 2025
      Guest Blogs

      Interview With Matias Madou – CTO and Co-Founder at Secure Code Warrior by Shauli Zacks

      26 August 2025

      LEAVE A REPLY Cancel reply

      Log in to leave a comment

      Most Popular

      Google Photos is getting a fun new sticker feature, but it’s not on Android

      26 August 2025

      I tried popular AI editing apps on Android — only one earned a spot on my phone

      26 August 2025

      Google is testing a new Discover feed feature for all you multilinguists out there

      26 August 2025

      Nothing seemingly caught passing off stock photos as Phone 3 camera samples

      26 August 2025
      Load more
      Algomaster
      Algomaster
      202 POSTS0 COMMENTS
      https://blog.algomaster.io
      Calisto Chipfumbu
      Calisto Chipfumbu
      6613 POSTS0 COMMENTS
      http://cchipfumbu@gmail.com
      Dominic
      Dominic
      32236 POSTS0 COMMENTS
      http://wardslaus.com
      Milvus
      Milvus
      80 POSTS0 COMMENTS
      https://milvus.io/
      Nango Kala
      Nango Kala
      6609 POSTS0 COMMENTS
      neverop
      neverop
      0 POSTS0 COMMENTS
      https://geeksforgeeks.org
      Nicole Veronica
      Nicole Veronica
      11779 POSTS0 COMMENTS
      Nokonwaba Nkukhwana
      Nokonwaba Nkukhwana
      11828 POSTS0 COMMENTS
      Safety Detectives
      Safety Detectives
      2579 POSTS0 COMMENTS
      https://www.safetydetectives.com/
      Shaida Kate Naidoo
      Shaida Kate Naidoo
      6719 POSTS0 COMMENTS
      Ted Musemwa
      Ted Musemwa
      7002 POSTS0 COMMENTS
      Thapelo Manthata
      Thapelo Manthata
      6678 POSTS0 COMMENTS
      Umr Jansen
      Umr Jansen
      6690 POSTS0 COMMENTS

      EDITOR PICKS

      Google Photos is getting a fun new sticker feature, but it’s not on Android

      26 August 2025

      I tried popular AI editing apps on Android — only one earned a spot on my phone

      26 August 2025

      Google is testing a new Discover feed feature for all you multilinguists out there

      26 August 2025

      POPULAR POSTS

      Google Photos is getting a fun new sticker feature, but it’s not on Android

      26 August 2025

      I tried popular AI editing apps on Android — only one earned a spot on my phone

      26 August 2025

      Google is testing a new Discover feed feature for all you multilinguists out there

      26 August 2025

      POPULAR CATEGORY

      • Languages45985
      • Data Modelling & AI17565
      • Java15156
      • Android13857
      • Mobile12983
      • Javascript12713
      • Guest Blogs12661
      • Data Structure & Algorithm10077
      Logo

      ABOUT US

      We provide you with the latest breaking news and videos straight from the technology industry.

      Contact us: hello@geeksforgeeks.org

      FOLLOW US

      Blogger
      Facebook
      Flickr
      Instagram
      VKontakte

      © NeverOpen 2022

      • Home
      • News
      • Data Modelling & AI
      • Mobile
      • Languages
      • Guest Blogs
      • Discussion
      • Our Team