Open PortfolioOpen Portfolio.
โ† Back to Blog

Building a Real-Time Chat Application

April 18, 2026at 2:00 PM UTCBy Pocket Portfolio TeamInnovation
Building a Real-Time Chat Application
#real-time#chat#application#web development

Building a real-time chat application is a common requirement in modern web development. This guide will walk you through building a basic real-time chat application using Node.js and Socket.io, a library that enables real-time, bidirectional communication between web clients and servers.

Problem

Traditional HTTP request/response cycles are not efficient for real-time communication. In a chat application, you need to push messages to users in real-time without them having to refresh the page or send repeated requests to the server.

Solution with Code

To solve this problem, we will use WebSockets, which allow persistent connections between the client and server. Socket.io, a popular library, abstracts WebSockets and provides a simple API.

Step 1: Set Up the Server

First, initialize a Node.js application and install the necessary dependencies:

npm init -y
npm install express socket.io

Create a file named server.js:

const express = require('express');
const http = require('http');
const { Server } = require('socket.io');

const app = express();
const server = http.createServer(app);
const io = new Server(server);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
  console.log('a user connected');
  
  socket.on('chat message', (msg) => {
    io.emit('chat message', msg);
  });

  socket.on('disconnect', () => {
    console.log('user disconnected');
  });
});

server.listen(3000, () => {
  console.log('listening on *:3000');
});

Step 2: Create the Client

Create an index.html file in the same directory:

<!DOCTYPE html>
<html>
  <head>
    <title>Chat</title>
  </head>
  <body>
    <ul id="messages"></ul>
    <form id="form" action="">
      <input id="input" autocomplete="off" /><button>Send</button>
    </form>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io();
      var form = document.getElementById('form');
      var input = document.getElementById('input');

      form.addEventListener('submit', function(e) {
        e.preventDefault();
        if (input.value) {
          socket.emit('chat message', input.value);
          input.value = '';
        }
      });

      socket.on('chat message', function(msg) {
        var item = document.createElement('li');
        item.textContent = msg;
        document.getElementById('messages').appendChild(item);
        window.scrollTo(0, document.body.scrollHeight);
      });
    </script>
  </body>
</html>

Step 3: Run Your Application

Start the server:

node server.js

Open http://localhost:3000 in multiple browser windows and test the chat by sending messages.

Key Concepts

  • WebSockets: These provide full-duplex communication channels over a single TCP connection, enabling real-time data exchange.
  • Socket.io: A library that simplifies the use of WebSockets by providing fallbacks for environments where WebSockets are not supported.
  • Node.js: A JavaScript runtime built on Chrome's V8 engine, suitable for building scalable network applications.

This simple chat application demonstrates the power of real-time web technologies and can be expanded with additional features such as user authentication, chat rooms, and message persistence.

Building a Real-Time Chat Application | Open Portfolio Blog | Open Portfolio