Building a Real-Time Dashboard with Node.js and Socket.IO

Building a real-time dashboard can be a powerful way to monitor and visualize data in real-time. In this article, we will explore how to build a real-time dashboard using Node.js and Socket.IO.

What is a Real-Time Dashboard?

A real-time dashboard is a web application that displays data in real-time. The data can come from a variety of sources, such as a database, APIs, or even live data feeds. The purpose of a real-time dashboard is to provide a visual representation of data that can be easily monitored and understood in real-time.

Building a Real-Time Dashboard with Node.js and Socket.IO

Node.js is a great platform for building real-time applications because it is event-driven and non-blocking. This means that Node.js can handle a large number of simultaneous connections and respond to events in real-time. Socket.IO is a popular library for real-time communication between clients and servers that is built on top of Node.js.

To build a real-time dashboard with Node.js and Socket.IO, we will need to create a Node.js server that listens for data and broadcasts it to connected clients. We will also need to create a client-side application that receives data from the server and displays it in real-time.

Step 1: Set Up the Server

First, we will create a new directory for our project and initialize a new Node.js project using npm:

mkdir real-time-dashboard
cd real-time-dashboard
npm init -y

Next, we will install the necessary dependencies for our project:

npm install express socket.io --save

Now, we can create a new file called “app.js” and add the following code:

const express = require("express");
const http = require("http");
const socketIO = require("socket.io");

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

const port = process.env.PORT || 3000;


io.on("connection", function(socket) {
  console.log("Client connected");

  socket.on("disconnect", function() {
    console.log("Client disconnected");

server.listen(port, function() {
  console.log("Server running on port " + port);

This code creates an Express application and a Socket.IO server. It also listens for connections from clients and logs messages to the console when clients connect and disconnect.

Step 2: Set Up the Client

Next, we will create a client-side application that connects to the server and receives real-time data. Create a new directory called “public” in your project directory. Inside the “public” directory, create a new file called “index.html” and add the following code:

<!doctype html>
    <title>Real-Time Dashboard</title>
    <script src="/socket.io/socket.io.js"></script>
    <h1>Real-Time Dashboard</h1>
    <ul id="messages"></ul>

    <script src="app.js"></script>

This code sets up the basic structure of our real-time dashboard, including an unordered list for displaying data.

Create a new file called “app.js” in the “public” directory and add the following code:

const socket = io();

const messagesList = document.getElementById("messages");

socket.on("data", function(data) {

function addDataToUI(data) {
  const li = document.createElement("li");
  li.innerHTML = data;

This code sets up a Socket.IO client that listens for data from the server and adds it to the UI.

Step 3: Broadcast Data

Now that we have set up the server and client, we can broadcast data from the server to the client in real-time. To do this, we will modify the server-side code to emit data to all connected clients.

Replace the “io.on” function in “app.js” with the following code:

let data = 0;

setInterval(function() {
  io.emit("data", data);
}, 1000);

This code creates a variable called “data” and increments it every second. It then emits the “data” event to all connected clients using the “io.emit” function.

Now, when you run the server using the command “node app.js” and open the client-side application in your web browser, you should see data being displayed in real-time in the dashboard.


In this article, we have explored how to build a real-time dashboard using Node.js and Socket.IO. We have learned how to set up a server that broadcasts data to connected clients, and how to create a client-side application that receives real-time data and displays it in the UI. With these skills, you can build all kinds of real-time applications that can be used for monitoring and visualization of data.

Trả lời

Email của bạn sẽ không được hiển thị công khai.