<?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 Ganesh Chincholkar on Medium]]></title>
        <description><![CDATA[Stories by Ganesh Chincholkar on Medium]]></description>
        <link>https://medium.com/@ganeshpc?source=rss-45112b16ae7c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lPcS71rKRNaAGtssIXOohA.png</url>
            <title>Stories by Ganesh Chincholkar on Medium</title>
            <link>https://medium.com/@ganeshpc?source=rss-45112b16ae7c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 11:11:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ganeshpc/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[Building a Simple Real-Time Group Chat Application Using WebSockets in JavaScript]]></title>
            <link>https://medium.com/@ganeshpc/building-a-simple-real-time-group-chat-application-using-websockets-in-javascript-891bd30bb736?source=rss-45112b16ae7c------2</link>
            <guid isPermaLink="false">https://medium.com/p/891bd30bb736</guid>
            <category><![CDATA[https]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[group-chat]]></category>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[web-app-development]]></category>
            <dc:creator><![CDATA[Ganesh Chincholkar]]></dc:creator>
            <pubDate>Thu, 10 Oct 2024 19:36:28 GMT</pubDate>
            <atom:updated>2024-10-10T19:36:28.638Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/803/1*e4VsUnUEi80uCoPgfZmZ8w.png" /></figure><h4>Introduction:</h4><p>Ever wondered how to create a real-time group chat application from scratch? With WebSockets, it’s simpler than you might think! In this tutorial, we’ll walk through building a basic group chat using just JavaScript for both the frontend and backend. But before that let’s understand what websockets are.</p><h4>What are Websockets?</h4><p>WebSockets are a communication protocol that enables full-duplex communication channels over a single TCP connection. This means that <strong>both the client and server can send and receive messages simultaneously</strong>, without the overhead of constantly establishing new HTTP connections.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PeeYkVg1tPXgWj4Uxc9y5Q.jpeg" /></figure><h4>Key Features of Websockets</h4><ul><li><strong>Traditional HTTP Requests</strong> follow a request-response model where the client initiates the communication, and the server responds. Once the response is sent, the connection is closed.</li><li>In contrast, <strong>WebSockets</strong> allow persistent connections. After the initial handshake using HTTP (called an “Upgrade” request), the connection is upgraded to WebSockets, and data can flow in both directions without needing to reconnect for each message.</li></ul><h4>Key Features of WebSockets:</h4><ul><li><strong>Bi-Directional Communication:</strong> Both client and server can send and receive data at any time, reducing latency and enabling real-time interactions.</li><li><strong>Persistent Connection:</strong> Once established, the WebSocket connection remains open, which is optimal for real-time applications like chat, gaming, and live feeds.</li><li><strong>Lower Overhead:</strong> Unlike traditional polling techniques (e.g., long-polling or AJAX), WebSockets reduce the number of HTTP requests, making them more efficient in bandwidth use.</li></ul><p>Now we have some understanding about what websockets are let’s start with building the group chat application. This is going to be a simple web page where multiple people can post message on to single chat interface.</p><h4>Prerequisites:</h4><p><strong>Node.js </strong>installed <br>Basic understanding of <strong>JavaScript</strong> and <strong>HTML<br></strong>Familiarity with <strong>Node.js</strong>, but don’t worry, I’ll guide you through each step</p><h4>Why Use WebSockets?</h4><p><strong>WebSockets</strong> provide real-time, full-duplex communication between the client and server, making them perfect for chat applications. Unlike traditional HTTP requests, WebSockets maintain an open connection, allowing instant message delivery without the overhead of multiple requests. It’s exactly what you need for a seamless chat experience.</p><h4>How It Works</h4><ul><li><strong>Backend (Node.js)</strong>: We’ll set up a WebSocket server that listens for incoming messages. When a message is received from any client, the server broadcasts it to all connected clients.</li><li><strong>Frontend (HTML/JavaScript)</strong>: A simple chat interface lets users send and receive messages. When the server sends a message, it’s displayed to all clients in real-time.</li></ul><h4>Lets Start Building the Application</h4><p><strong>1. Backend<br></strong>We’ll use <strong>Node.js</strong> to build the WebSocket server.<br>Create an index.js file with a basic WebSocket server that broadcasts messages to all connected clients.</p><pre>const WebSocket = require(&#39;ws&#39;);<br><br>const webSocketServer = new WebSocket.Server({ port: 8080 });<br><br>webSocketServer.on(&#39;listening&#39;, () =&gt; {<br>    console.log(&#39;WebSocket server is listening on port 8080&#39;);<br>});<br><br>webSocketServer.on(&#39;connection&#39;, (webSocket) =&gt; {<br>    webSocket.on(&#39;message&#39;, (message) =&gt; {<br>        webSocketServer.clients.forEach((client) =&gt; {<br>            if (client.readyState === WebSocket.OPEN) {<br>                console.log(&quot;Message received: &quot; + message);<br>                client.send(message);<br>            }<br>        });<br>    });<br>});</pre><p><strong>2. Building the frontend<br></strong>The frontend will be a simple <strong>HTML/JavaScript</strong> file with an input field for users to type messages and a display area for the chat.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<br>    &lt;title&gt;Chat Room&lt;/title&gt;<br>    &lt;script defer src=&quot;socket.js&quot;&gt;&lt;/script&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;ul&gt;<br>    &lt;/ul&gt;<br><br>    &lt;input placeholder=&quot;message&quot;&gt;<br>    &lt;button&gt;Send&lt;/button&gt;  <br><br>    &lt;script&gt;<br>        // connect to the websocket server<br>        const webSocket = new WebSocket(&#39;ws://localhost:8080&#39;);<br><br>        // Handler for the message event event<br>        webSocket.onmessage = (socketMessage) =&gt; {<br>            console.log(socketMessage.data);<br>            const messageList = document.querySelector(&#39;ul&#39;);<br>            messageList.innerHTML += `&lt;li&gt;${socketMessage.data}&lt;/li&gt;`;<br>        };<br><br>        // handle the button click event<br>        document.querySelector(&#39;button&#39;).addEventListener(&#39;click&#39;, () =&gt; {<br>            const messageText = document.querySelector(&#39;input&#39;).value;<br>            webSocket.send(messageText);<br>        });<br>    &lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p><strong>3. Running the project</strong></p><ul><li><strong>Backend</strong>: Start the WebSocket server with node index.js. The server will now listen on <strong>port 8080</strong>.</li><li><strong>Frontend</strong>: Open frontend/index.html in multiple browser tabs to simulate different users. Type messages and see them broadcasted in real-time across all tabs.</li></ul><p><strong>While this is a simple chat room, you can extend it by:<br></strong>1. Adding <strong>user management</strong> (e.g., usernames, login).<br>2. Storing messages in a <strong>database</strong> for persistence.<br>3. Adding features like <strong>message encryption</strong>.<br>4. Deploying it to a cloud platform such as AWS or Heroku.</p><h3>Conclusion</h3><p>By using <strong>WebSockets</strong>, you can easily create real-time applications like chat rooms, games, or even live collaboration tools. This basic example is a foundation for more complex applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=891bd30bb736" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>