Creating a real-time dashboard involves using technologies that support bidirectional communication between the server and clients. In this example, we'll use Socket.io to achieve real-time communication. Follow the steps below to create a simple real-time dashboard using Node.js, Express, and Socket.io.
Step 1: Set Up Your Project
Create a new project folder:
mkdir real-time-dashboard cd real-time-dashboard
Initialize a new Node.js project:
npm init -y
Install necessary dependencies:
npm install express socket.io
Step 2: Create Your Folder Structure
Create the following folder structure:
real-time-dashboard/
|-- public/
| |-- styles/
| |-- main.css
|-- src/
| |-- routes/
| |-- index.js
| |-- app.js
|-- views/
| |-- dashboard.ejs
|-- server.js
Step 3: Set Up Express Server with Socket.io
In app.js
, set up an Express server with Socket.io:
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;
app.use(express.static('public'));
app.set('view engine', 'ejs');
// Set up routes
const indexRoutes = require('./routes/index');
app.use('/', indexRoutes);
// Socket.io connection
io.on('connection', (socket) => {
console.log('A user connected');
// Example: Send initial data to the client
socket.emit('initialData', { value: 'Welcome to the real-time dashboard!' });
// Example: Handle client updates
socket.on('updateData', (data) => {
console.log('Received update:', data);
// Broadcast the update to all connected clients
io.emit('broadcastUpdate', data);
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
module.exports = { server, io };
Step 4: Create a Route and View
In routes/index.js
, create a route to render the dashboard view:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.render('dashboard');
});
module.exports = router;
Create a new file views/dashboard.ejs
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/styles/main.css">
<title>Real-time Dashboard</title>
</head>
<body>
<h1>Real-time Dashboard</h1>
<div id="data-container">
<p id="data-value">Waiting for updates...</p>
</div>
<script src="https://cdn.socket.io/4.1.2/socket.io.min.js"></script>
<script>
const socket = io();
// Example: Receive initial data from the server
socket.on('initialData', (data) => {
document.getElementById('data-value').innerText = data.value;
});
// Example: Send updates to the server
const newData = { value: 'New data from the client!' };
socket.emit('updateData', newData);
// Example: Receive broadcasted updates from the server
socket.on('broadcastUpdate', (data) => {
console.log('Received update from the server:', data);
document.getElementById('data-value').innerText = data.value;
});
</script>
</body>
</html>
Step 5: Create CSS Styles
In public/styles/main.css
, add your styles:
body {
font-family: 'Arial', sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
#data-container {
margin-top: 20px;
}
#data-value {
font-size: 18px;
color: #007BFF;
}
Step 6: Run Your Application
In server.js
, use the exported server to start the application:
const { server } = require('./src/app');
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Start the server:
node server.js
Visit http://localhost:3000 in your browser. You should see the real-time dashboard displaying initial data. Open the same page in multiple browser tabs to simulate multiple clients. As you update data in one tab, the changes should be reflected in real-time across all open tabs.
This example is a basic demonstration of real-time communication using Socket.io. Depending on your requirements, you can extend the dashboard with more sophisticated features and data sources.