Skip to content

Commit

Permalink
Create realTimeAnalytics.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 4, 2024
1 parent 143320c commit 2cc2e90
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/analytics/realTimeAnalytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// realTimeAnalytics.js

import WebSocket from 'ws';

class RealTimeAnalytics {
constructor(port) {
this.port = port;
this.wss = new WebSocket.Server({ port: this.port });
this.metrics = {}; // Object to store key metrics
this.clients = new Set(); // Set to store connected clients

this.initializeWebSocket();
}

// Initialize WebSocket server
initializeWebSocket() {
this.wss.on('connection', (ws) => {
console.log('New client connected');
this.clients.add(ws);

// Send current metrics to the newly connected client
ws.send(JSON.stringify(this.metrics));

// Handle client disconnection
ws.on('close', () => {
console.log('Client disconnected');
this.clients.delete(ws);
});
});

console.log(`WebSocket server is running on ws://localhost:${this.port}`);
}

// Update a specific metric
updateMetric(key, value) {
this.metrics[key] = value;
console.log(`Updated metric: ${key} = ${value}`);
this.broadcastMetrics();
}

// Broadcast updated metrics to all connected clients
broadcastMetrics() {
const metricsData = JSON.stringify(this.metrics);
this.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(metricsData);
}
});
}

// Example usage
exampleUsage() {
// Simulate metric updates
setInterval(() => {
const randomUsers = Math.floor(Math.random() * 100);
const randomSales = (Math.random() * 1000).toFixed(2);
this.updateMetric('activeUsers', randomUsers);
this.updateMetric('totalSales', randomSales);
}, 5000); // Update every 5 seconds
}
}

// Example usage
const analyticsPort = 8080; // Port for WebSocket server
const realTimeAnalytics = new RealTimeAnalytics(analyticsPort);
realTimeAnalytics.exampleUsage();

export default RealTimeAnalytics;

0 comments on commit 2cc2e90

Please sign in to comment.