<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nimesh Madhusanka on Medium]]></title>
        <description><![CDATA[Stories by Nimesh Madhusanka on Medium]]></description>
        <link>https://medium.com/@madhusanka2023?source=rss-5a9fe6767653------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VY3Jv3_XRb7gNnL9xsU_cQ.png</url>
            <title>Stories by Nimesh Madhusanka on Medium</title>
            <link>https://medium.com/@madhusanka2023?source=rss-5a9fe6767653------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 10:59:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@madhusanka2023/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Beginner’s Guide to WebSockets and Real-Time Web]]></title>
            <link>https://madhusanka2023.medium.com/the-beginners-guide-to-websockets-and-real-time-web-bb626b59de49?source=rss-5a9fe6767653------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb626b59de49</guid>
            <dc:creator><![CDATA[Nimesh Madhusanka]]></dc:creator>
            <pubDate>Mon, 25 Aug 2025 04:28:04 GMT</pubDate>
            <atom:updated>2025-08-25T04:28:04.399Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*jlpN9KXBLpE7d2SHGo53BQ.jpeg" /></figure><h3>What is WebSocket?</h3><p>Imagine you’re chatting with a friend online. Instead of sending a message, waiting, and refreshing the page to see if they replied, you want the messages to appear instantly — just like in real life. That’s exactly what <strong>WebSocket</strong> does.</p><p>WebSocket is a special technology that creates a <strong>continuous two-way connection</strong> between your browser and the server. Once the connection is open, both sides can send and receive information instantly — without waiting or refreshing.</p><p>That’s why WebSockets are used in things like <strong>chat apps, online games, live notifications, and stock price updates</strong>, where speed really matters.</p><h3>How WebSockets Work</h3><h3>1. Establishing a Connection (Handshake)</h3><p>The process begins with an <strong>HTTP handshake</strong>. The client (e.g., a browser) sends an HTTP request to the server indicating that it wants to upgrade the connection to WebSocket by including special headers:</p><pre>Upgrade: websocket  <br>Connection: Upgrade</pre><p>If the server supports WebSocket, it responds with <strong>status code 101 (Switching Protocols)</strong> to confirm the upgrade. At this point, the connection transitions from HTTP to WebSocket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-1nCdmY2ptCrQR2hRKV0og.png" /><figcaption>return 101 status code in network tab</figcaption></figure><h3>2. Persistent, Bidirectional Channel</h3><p>After the handshake, a <strong>single, persistent TCP connection</strong> is established. Unlike HTTP — where the client must request data each time — WebSockets allow both the client and server to send messages to each other <strong>anytime, without reopening the connection</strong>.</p><p>This makes communication faster and more efficient, especially for real-time applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EUa_Ptjj6rIwgJiQ2jI4Ig.png" /></figure><h3>3. Communication (Full-Duplex)</h3><p>Once the WebSocket connection is active, both sides can exchange messages freely. This is called <strong>full-duplex communication</strong>, meaning data can flow <strong>in both directions simultaneously</strong>.</p><p>Messages are transmitted in the form of <strong>frames</strong>:</p><ul><li><strong>Header</strong> → contains metadata (length, type, control info).</li><li><strong>Payload</strong> → carries the actual data (text or binary).</li></ul><p>This lightweight design eliminates the overhead of repeatedly opening and closing HTTP requests.</p><h3>4. Closing the Connection</h3><p>Ending a WebSocket session is done through a process called the <strong>closing handshake</strong>. When one party (either the client or the server) decides to close the connection, it sends a special message known as a <strong>close frame</strong>. This frame can also include a status code or a reason for closing.</p><p>The other party then responds with its own close frame to <strong>acknowledge the closure</strong>. Once both sides have exchanged these messages, the connection is safely terminated.</p><p>This graceful process ensures that:</p><ul><li>Any remaining data is delivered before closing.</li><li>Both sides agree on the termination.</li><li>Abrupt disconnections (which could cause data loss) are avoided.</li></ul><p>Think of it like a <strong>phone call</strong>.</p><ul><li>One person says <em>“Goodbye”</em>.</li><li>The other person replies <em>“Goodbye”</em>.</li><li>Only then do both hang up the call.</li></ul><p>This way, the conversation ends smoothly without cutting off anyone mid-sentence.</p><h4>Why Use WebSockets Instead of HTTP?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_rtlRsZ85OjX6a_5WMS7WQ.png" /></figure><h4>Conclusion</h4><p>WebSockets are the backbone of the <strong>real-time web</strong>. They provide fast, lightweight, two-way communication that traditional HTTP cannot match.</p><p>Whether you’re building a <strong>chat app, multiplayer game, or live dashboard</strong>, WebSockets let your users experience instant interactions — just like real life.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb626b59de49" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>