DEV Community

Manthan Ankolekar
Manthan Ankolekar

Posted on

Building a real-time chat application using Node.js, MongoDB, and Express

Let's get started!


  1. Node.js and npm installed on your system.
  2. Basic understanding of JavaScript and Node.js.

Step 1: Set up the Project

Create a new project folder and initialize a new Node.js project with npm.

mkdir real-time-chat-app
cd real-time-chat-app
npm init -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Dependencies

Install the necessary packages for our application.

npm install express mongoose
Enter fullscreen mode Exit fullscreen mode

Step 3: Set up the Server

Create an app.js file in the root of your project and set up the server using Express and

// app.js

const express = require('express');
const http = require('http');
const socketIO = require('');
const mongoose = require('mongoose');

// Connect to MongoDB
mongoose.connect('your_mongodb_uri', {
  useNewUrlParser: true,
  useUnifiedTopology: true,

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

const PORT = 3000;

// Start the server
server.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
Enter fullscreen mode Exit fullscreen mode

Step 4: Create MongoDB Schema

In this example, we'll create a simple schema for storing chat messages. Create a new folder called models in your project's root and add a Message.js file with the following content:

// models/Message.js

const mongoose = require('mongoose');

const messageSchema = new mongoose.Schema({
  user: String,
  text: String,
  timestamp: { type: Date, default: },

const Message = mongoose.model('Message', messageSchema);

module.exports = Message;
Enter fullscreen mode Exit fullscreen mode

Step 5: Implement for Real-Time Chat

Now, we'll implement the logic to handle real-time chat functionality.

// app.js

// Add these lines after the previous code in app.js

const Message = require('./models/Message');

io.on('connection', (socket) => {
  console.log('User connected:',;

  // Listen for incoming chat messages
  socket.on('chat message', (data) => {
    console.log('Received message:', data);

    // Save the message to MongoDB
    const message = new Message({ user: data.user, text: data.message }); => {
      if (err) {
        console.error('Error saving message to database:', err);
      } else {
        console.log('Message saved to the database');

    // Broadcast the message to all connected clients
    io.emit('chat message', data);

  // Listen for user disconnection
  socket.on('disconnect', () => {
    console.log('User disconnected:',;
Enter fullscreen mode Exit fullscreen mode

Step 6: Create Frontend

Now, create a basic HTML file to interact with the server using

<!-- index.html -->

<!DOCTYPE html>
  <title>Real-Time Chat</title>
  <h1>Real-Time Chat App</h1>
  <div id="chat">
    <ul id="messages"></ul>
    <form id="messageForm">
      <input id="messageInput" autocomplete="off" />

  <script src="/"></script>
    const socket = io();

    // Handle incoming chat messages
    socket.on('chat message', (data) => {
      const messages = document.getElementById('messages');
      const li = document.createElement('li');
      li.textContent = `${data.user}: ${data.message}`;

    // Handle form submission
    document.getElementById('messageForm').addEventListener('submit', (e) => {
      const input = document.getElementById('messageInput');
      const message = input.value.trim();
      if (message !== '') {
        socket.emit('chat message', { user: 'User', message });
        input.value = '';
Enter fullscreen mode Exit fullscreen mode

Step 7: Serving Static Files

Create a folder called public in your project's root directory and place the index.html file inside it. Additionally, modify the app.js to serve static files from this folder.

// app.js

// Add these lines after the previous code in app.js

Enter fullscreen mode Exit fullscreen mode

Step 8: Run the Application

Now, you can run your real-time chat application using the following command:

node app.js
Enter fullscreen mode Exit fullscreen mode

Open your browser and visit http://localhost:3000. You can open multiple browser tabs to simulate multiple users and see real-time chat in action.

Remember, this example is a basic implementation. In a real-world scenario, you may need to handle user authentication, store messages more efficiently, and add various other features to make the chat application more robust and secure.

Complete Code: GitHub

Top comments (0)