<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Angel Grace Arapoc</title>
    <description>The latest articles on DEV Community by Angel Grace Arapoc (@angel_arapoc).</description>
    <link>https://dev.to/angel_arapoc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3081864%2Fa71fcdaa-d543-4d48-b09e-3d7b20ade501.jpg</url>
      <title>DEV Community: Angel Grace Arapoc</title>
      <link>https://dev.to/angel_arapoc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/angel_arapoc"/>
    <language>en</language>
    <item>
      <title>Building a simple Real-Time Chat App with Socket.IO</title>
      <dc:creator>Angel Grace Arapoc</dc:creator>
      <pubDate>Thu, 24 Apr 2025 13:57:14 +0000</pubDate>
      <link>https://dev.to/up_min_sparcs/building-a-simple-real-time-chat-app-with-socketio-15</link>
      <guid>https://dev.to/up_min_sparcs/building-a-simple-real-time-chat-app-with-socketio-15</guid>
      <description>&lt;p&gt;This article is co-authored by &lt;a class="mentioned-user" href="https://dev.to/zcalifornia"&gt;@zcalifornia&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  The Pulse of Real-Time Communication
&lt;/h2&gt;

&lt;p&gt;Picture this: you're playing a multiplayer game, sending a message to a friend, or watching live election results come in—all happening in real time. In today's world, speed isn't just a luxury—it’s a necessity. That’s where real-time communication apps come in. These apps allow instantaneous interactions between users and systems, creating seamless digital experiences we now expect in everything from social media to online customer service.&lt;/p&gt;

&lt;p&gt;So, what powers this magic behind the scenes? Enter: Socket.IO, a robust JavaScript library that bridges the gap between client and server in real time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-Time Communication Overview
&lt;/h2&gt;

&lt;p&gt;Real-time communication (RTC) allows real-time, interactive exchange of information among systems or users. WebSockets and the Socket.IO protocol offer long-lived connections, enabling direct message transmission with little delay. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8lqqm67sr0lcvryc107.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8lqqm67sr0lcvryc107.png" alt="Image description" width="512" height="256"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Use Cases of Real-Time Apps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chat apps like WhatsApp or Messenger&lt;/li&gt;
&lt;li&gt;Collaborative tools like Google Docs&lt;/li&gt;
&lt;li&gt;Live tracking and feeds such as stock tickers and delivery apps&lt;/li&gt;
&lt;li&gt;Online games that demand split-second communication&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;In this tutorial, we’ll walk you through building a basic real-time chat app using Node.js and Socket.IO—a hands-on guide to understanding how instant messaging apps are born, byte by byte.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;📂 Want to explore or copy the full source code? You can find it all here: &lt;a href="https://github.com/angelara04/Realtime-Chat-App.git" rel="noopener noreferrer"&gt;Realtime Chat App on GitHub&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before we begin building the real-time chat app, ensure you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node.js&lt;/strong&gt;: This is required to run the server-side JavaScript and install the necessary dependencies. You can download and install the latest LTS version of Node.js from &lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To verify that Node.js is installed correctly, open your terminal/command prompt and run the following commands:&lt;br&gt;
&lt;code&gt;node -v&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm -v&lt;/code&gt;&lt;br&gt;
If the above commands return version numbers (e.g., v16.13.0 for Node.js), you're good to go!”&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code editor&lt;/strong&gt;: You'll need a text editor like VS Code or Sublime Text to edit your code.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. Project Setup: Lean and Efficient
&lt;/h2&gt;

&lt;p&gt;Our minimalist structure keeps everything organized without unnecessary complexity:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;chat-app/&lt;br&gt;
├── index.html&lt;br&gt;
├── script.js&lt;br&gt;
├── server.js&lt;br&gt;
├── package.json&lt;br&gt;
└── package-lock.json&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This folder will contain your entire project!&lt;/p&gt;

&lt;p&gt;Create a file called &lt;code&gt;index.html&lt;/code&gt; and paste this code into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset="UTF-8" /&amp;gt;
  &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
  &amp;lt;meta http-equiv="X-UA-Compatible" content="ie=edge" /&amp;gt;
  &amp;lt;title&amp;gt;Chat App&amp;lt;/title&amp;gt;

  &amp;lt;script defer src="http://localhost:3000/socket.io/socket.io.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script defer src="script.js"&amp;gt;&amp;lt;/script&amp;gt;

  &amp;lt;style&amp;gt;
    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
      background-color: #f4f6f8;
      display: flex;
      flex-direction: column;
      align-items: center;
      height: 100vh;
    }

    #message-container {
      width: 100%;
      max-width: 800px;
      flex-grow: 1;
      overflow-y: auto;
      padding: 1rem;
      display: flex;
      flex-direction: column;
    }

    #message-container .received,
    #message-container .sent {
      margin-bottom: 10px;
      padding: 10px 15px;
      border-radius: 15px;
      max-width: 70%;
      word-wrap: break-word;
      font-size: 1rem;
      display: inline-block;
      position: relative;
    }

    #message-container .received {
      align-self: flex-start;
      background-color: #e7d692;
      color: #504941;
      margin: 15px;
    }

    #message-container .sent {
      align-self: flex-end;
      background-color: #007bff;
      color: white;
      margin: 15px;
    }

    .timestamp {
      font-size: 0.75rem;
      color: #3c3c3c;
      position: absolute;
      bottom: -18px;
      right: 5px;
    }

    .system-message {
      text-align: center;
      color: #000;
      font-weight: bold;
      font-size: 1rem;
      margin: 10px 0;
    }

    #send-container {
      width: 100%;
      max-width: 800px;
      display: flex;
      padding: 10px;
      background-color: #ffffff;
      border-top: 1px solid #ccc;
      position: sticky;
      bottom: 0;
    }

    #message-input {
      flex-grow: 1;
      padding: 10px;
      font-size: 1rem;
      border: 1px solid #ccc;
      border-radius: 5px;
      margin-right: 10px;
    }

    #send-button {
      padding: 10px 20px;
      background-color: #007bff;
      color: white;
      font-size: 1rem;
      border: none;
      border-radius: 5px;
      cursor: pointer;
    }

    #send-button:hover {
      background-color: #0056b3;
    }
  &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

  &amp;lt;div id="message-container"&amp;gt;&amp;lt;/div&amp;gt;

  &amp;lt;form id="send-container"&amp;gt;
    &amp;lt;input type="text" id="message-input" placeholder="Type a message..." autocomplete="off" required /&amp;gt;
    &amp;lt;button type="submit" id="send-button"&amp;gt;Send&amp;lt;/button&amp;gt;
  &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Powering Up with Dependencies
&lt;/h2&gt;

&lt;p&gt;To set up the project, execute these commands:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm init -y&lt;br&gt;
 npm install socket.io&lt;br&gt;
 npm install --save-dev nodemon&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What Do These Commands Do?&lt;/p&gt;

&lt;p&gt;When setting up your chat app, you’ll run three commands in the terminal. Think of these like setting up your tools before you build a LEGO project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm init -y&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What it does:&lt;/p&gt;

&lt;p&gt;This creates a file called package.json. It’s like a notebook that keeps track of what your app needs and how to run it.&lt;/p&gt;

&lt;p&gt;What you’ll see:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54f52bift399cx5bh0gu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54f52bift399cx5bh0gu.png" alt="Image description" width="512" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm install socket.io&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What it does:&lt;/p&gt;

&lt;p&gt;This installs Socket.IO, the special tool that helps different users chat in real-time by connecting browsers and the server like walkie-talkies.&lt;/p&gt;

&lt;p&gt;What you’ll see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fid40d2d9dk87zcpjf2h1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fid40d2d9dk87zcpjf2h1.png" alt="Image description" width="781" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm install --save-dev nodemon&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What it does:&lt;/p&gt;

&lt;p&gt;This adds nodemon, a helper tool that watches your code and automatically restarts the server every time you make a change. Like having a robot that refreshes your work without you doing it manually.&lt;/p&gt;

&lt;p&gt;What you’ll see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5dwfshn3i9gobm4qxlab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5dwfshn3i9gobm4qxlab.png" alt="Image description" width="800" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What Happens Next?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Once you’ve run the necessary commands, your folder will be fully set up and ready to power the chat app. From there, you can move on to the next steps: build the server, open two browser tabs, and watch messages sync instantly between them—real-time chatting in action, just like magic!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjcwxip0zm9fn26hdq635.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjcwxip0zm9fn26hdq635.png" alt="Image description" width="681" height="435"&gt;&lt;/a&gt;&lt;br&gt;
The diagram above illustrates the message flow in a Socket.IO chat application. The corresponding code implementation will be discussed in the following sections.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Server Setup: Real-Time Core
&lt;/h2&gt;

&lt;p&gt;The following code is like the brain of a chat room. It runs on a computer (the "server") and helps people talk to each other instantly.&lt;/p&gt;

&lt;p&gt;Here’s what it does, in chronological order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Starts a server so people can connect to it.&lt;/li&gt;
&lt;li&gt;Keeps track of who's online by remembering their names.&lt;/li&gt;
&lt;li&gt;When someone joins, it tells everyone else, “Hey, a new person just came in!”&lt;/li&gt;
&lt;li&gt;When someone sends a message, it grabs the message, adds the time, and shares it with the others.&lt;/li&gt;
&lt;li&gt;When someone leaves, it lets everyone know that person has gone.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically, it's like the host of a group chat—it watches who comes and goes and makes sure everyone sees the messages in real time. See the implementation below!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;server.js&lt;/code&gt; - Our Socket.IO-powered backend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Import and initialize Socket.IO on port 3000
const io = require('socket.io')(3000);


// Create an object to store connected users
const users = {};


// Listen for new client connections
io.on('connection', socket =&amp;gt; {

  // When a new user joins, store their name using their socket ID
  socket.on('new-user', name =&amp;gt; {
    users[socket.id] = name;


    // Notify all other clients that a new user has connected
    socket.broadcast.emit('user-connected', name);
  });


  // When a user sends a chat message
  socket.on('send-chat-message', message =&amp;gt; {
    // Get the current time as a readable timestamp (e.g. "09:45 AM")
    const timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });


    // Broadcast the message to all other users along with the sender's name and timestamp
    socket.broadcast.emit('chat-message', {
      message: message,
      name: users[socket.id],
      time: timestamp
    });
  });


  // When a user disconnects
  socket.on('disconnect', () =&amp;gt; {
    // Notify others that the user has left
    socket.broadcast.emit('user-disconnected', users[socket.id]);


    // Remove the user from the users object
    delete users[socket.id];
  });


});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Handling Client-Side Connections
&lt;/h2&gt;

&lt;p&gt;To enable real-time communication, connect to the server using Socket.IO in the client-side JavaScript (&lt;code&gt;script.js&lt;/code&gt;). In the current implementation, the connection URL for the WebSocket server is hardcoded in the &lt;code&gt;script.js&lt;/code&gt; file as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const socket = io('http://localhost:3000');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We add the DOM elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const messageContainer = document.getElementById('message-container');
const messageForm = document.getElementById('send-container');
const messageInput = document.getElementById('message-input');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;User name prompt&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4eex28yflsc09afle5d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4eex28yflsc09afle5d.png" alt="Image description" width="551" height="240"&gt;&lt;/a&gt;&lt;br&gt;
Before connecting to the server, the user is prompted to enter their name. Here's how this works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Prompt user for their name and notify server
const name = prompt('What is your name?');
socket.emit('new-user', name);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line triggers a prompt asking for the user's name. &lt;/p&gt;

&lt;p&gt;Then, we set up listeners to receive and log messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Receive a chat message from another user
socket.on('chat-message', data =&amp;gt; {
  appendMessage(`${data.name}: ${data.message}`, 'received', data.time);
});


// Notify when a user connects to the chat
socket.on('user-connected', name =&amp;gt; {
  appendSystemMessage(`${getTime()} - ${name} joined`);
});


// Notify when a user disconnects from the chat
socket.on('user-disconnected', name =&amp;gt; {
  appendSystemMessage(`${getTime()} - ${name} disconnected`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These handlers append messages and connection updates to the DOM. The script is linked in &lt;code&gt;index.html&lt;/code&gt; with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script defer src="http://localhost:3000/socket.io/socket.io.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script defer src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Sending and Receiving Messages
&lt;/h2&gt;

&lt;p&gt;Users send messages via a form. This form is handled in &lt;code&gt;script.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Handle sending a message
messageForm.addEventListener('submit', e =&amp;gt; {
  e.preventDefault(); // Prevent form from refreshing the page
  const message = messageInput.value;

  // Show the message in your chat window
  appendMessage(`You: ${message}`, 'sent', getTime());

  // Send the message to the server
  socket.emit('send-chat-message', message);

  // Clear the input box
  messageInput.value = '';
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server (&lt;code&gt;server.js&lt;/code&gt;) listens for this event:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;socket.on('send-chat-message', message =&amp;gt; {
  const timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  socket.broadcast.emit('chat-message', {
    message: message,
    name: users[socket.id],
    time: timestamp
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When received, the server broadcasts the message with the sender’s name and timestamp. Each client appends the message using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function appendMessage(text, type, timestamp) {
  const messageElement = document.createElement('div');
  messageElement.classList.add(type); // 'sent' or 'received'
  messageElement.innerText = text;


  const timestampElement = document.createElement('div');
  timestampElement.classList.add('timestamp');
  timestampElement.innerText = timestamp;


  messageElement.appendChild(timestampElement);
  messageContainer.appendChild(messageElement);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Adding Timestamps to Messages
&lt;/h2&gt;

&lt;p&gt;Timestamps are additional features to our Real-Time Chat App. Timestamps enhance context and readability in chat messages. They are generated and attached on the server using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const timestamp = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This formatted time (e.g., "09:45 AM") is included in the data sent from the server to clients.&lt;/p&gt;

&lt;p&gt;On the client side, we display the timestamp with each message:&lt;br&gt;
appendMessage(&lt;code&gt;${data.name}: ${data.message}&lt;/code&gt;, 'received', data.time);&lt;br&gt;
Additionally, we use a helper function to get the current time for local messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getTime() {
  return new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function uses the built-in Date object with formatting options. You may customize it to use 24-hour format or include seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Displaying Messages in the Interface
&lt;/h2&gt;

&lt;p&gt;A chat app isn't complete without a clear, readable way to display messages—and that’s where our client-side interface comes in. Using JavaScript and a touch of CSS, we dynamically render each message into the DOM to keep the conversation flowing smoothly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Structuring the Message&lt;/strong&gt;&lt;br&gt;
Messages are inserted into a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; container (#message-container) every time a user sends or receives a chat. Each message appears as its own block, styled based on whether it was sent by the current user or another participant. Here's how we handle that in &lt;code&gt;script.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function appendMessage(text, type, timestamp) {
  const messageElement = document.createElement('div');
  messageElement.classList.add(type); // 'sent' or 'received'
  messageElement.innerText = text;


  const timestampElement = document.createElement('div');
  timestampElement.classList.add('timestamp');
  timestampElement.innerText = timestamp;


  messageElement.appendChild(timestampElement);
  messageContainer.appendChild(messageElement);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type determines if the message aligns to the left (received) or right (sent).Each message includes a timestamp to provide temporal context.The messageContainer keeps stacking these messages in real time, creating a chat thread.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;System Notifications&lt;/strong&gt;&lt;br&gt;
To enhance user awareness, the interface also includes system messages—like when someone joins or leaves the chat:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function appendSystemMessage(text) {
  const systemMessage = document.createElement('div');
  systemMessage.classList.add('system-message');
  systemMessage.innerText = text;
  messageContainer.appendChild(systemMessage);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These notifications are styled differently to stand out and maintain clarity in the conversation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Styling the Interface&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;index.html&lt;/code&gt; file includes CSS rules to visually separate incoming and outgoing messages using color and alignment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    #message-container .received {
      align-self: flex-start;
      background-color: #e7d692;
      color: #504941;
      margin: 15px;
    }

    #message-container .sent {
      align-self: flex-end;
      background-color: #007bff;
      color: white;
      margin: 15px;
    }

    .timestamp {
      font-size: 0.75rem;
      color: #3c3c3c;
      position: absolute;
      bottom: -18px;
      right: 5px;
    }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the chat a friendly, readable design while subtly indicating message origin. System messages are bold, centered, and unintrusive:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.system-message {
  text-align: center;
  color: #000;
  font-weight: bold;
  font-size: 1rem;
  margin: 10px 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how your Real-Time Chat App should appear from the perspective of two users in the chat:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5tec004jbz8lergk4el.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg5tec004jbz8lergk4el.png" alt="Image description" width="800" height="99"&gt;&lt;/a&gt;&lt;br&gt;
The left tab is Alex’s who joined earlier and the right tab is Cody who joined at 10:20PM.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. How do I run this app?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Go to the Project Folder&lt;/strong&gt;: Open your terminal (or command prompt), and navigate to the project folder Chat-App. This is the folder that contains the app files. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Start the App&lt;/strong&gt;: In the terminal, type the following command to run the app:&lt;br&gt;
&lt;code&gt;npm run start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Open the App in Two Browser Tabs&lt;/strong&gt;: Once the app starts, open the &lt;code&gt;index.html&lt;/code&gt; file in two separate tabs in your web browser.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Enter Different Usernames&lt;/strong&gt;: In each browser tab, choose a different username for chatting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Start Chatting&lt;/strong&gt;: Now, you can send messages! Thanks to Socket.IO, the messages you send from one tab will appear instantly in the other tab. This shows the real-time chat feature in action!&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Understanding Socket.IO: Features, Use Cases, and Limitations
&lt;/h2&gt;

&lt;p&gt;Socket.IO isn't just a fancy tool—it's the backbone of countless real-time services.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time bidirectional communication: Send and receive messages without refreshing.&lt;/li&gt;
&lt;li&gt;Fallback to long-polling: Keeps working even if WebSockets aren't supported.&lt;/li&gt;
&lt;li&gt;Room and namespace support: Helps organize users into groups or chats.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use Cases:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chat apps like Slack&lt;/li&gt;
&lt;li&gt;Live collaboration (e.g., Google Docs)&lt;/li&gt;
&lt;li&gt;Real-time games like multiplayer shooters&lt;/li&gt;
&lt;li&gt;Dashboards for stock markets, weather, or sports scores&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Limitations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not ideal for huge-scale apps without proper load balancing.&lt;/li&gt;
&lt;li&gt;Depends on a persistent connection—can be affected by unstable networks.&lt;/li&gt;
&lt;li&gt;Resource-heavy if not optimized properly.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;While Socket.IO is a brilliant tool, keep in mind that with great power comes the need for smart management.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  11. Conclusion and Next Steps
&lt;/h2&gt;

&lt;p&gt;Congratulations! You just built a real-time chat app from the ground up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up a Node.js + Express server&lt;/li&gt;
&lt;li&gt;Integrated Socket.IO for real-time messaging&lt;/li&gt;
&lt;li&gt;Added message timestamps and client-side DOM updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Real-Time = Real Impact&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Real-time features turn static sites into living experiences. Whether it’s a friendly “hi” or an urgent update, every message matters more when it arrives now.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What’s Next?&lt;/strong&gt;&lt;br&gt;
Take your app to the next level by exploring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Usernames or nicknames for identity&lt;/li&gt;
&lt;li&gt;Chat rooms or private DMs&lt;/li&gt;
&lt;li&gt;Typing indicators&lt;/li&gt;
&lt;li&gt;Message history storage with a database like MongoDB or PostgreSQL&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Each addition brings your app one step closer to the real-world products millions use every day.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  12. Resources
&lt;/h2&gt;

&lt;p&gt;Want to dive deeper? Here’s a list of official documentation and helpful links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📘 &lt;a href="https://socket.io/docs/v4/" rel="noopener noreferrer"&gt;Socket.IO Documentation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🟩 &lt;a href="https://nodejs.org/docs/latest/api/" rel="noopener noreferrer"&gt;Node.js Documentation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;💻 &lt;a href="https://github.com/angelara04/Realtime-Chat-App.git" rel="noopener noreferrer"&gt;GitHub Repo&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Whether you’re building your own chat app or integrating real-time features into your next big idea, this tutorial is your first step into a world where now really means now.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>socketio</category>
      <category>realtime</category>
      <category>chatapp</category>
    </item>
  </channel>
</rss>
