-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (45 loc) · 1.58 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const { createClient } = require('redis'); // Import Redis client
// Initialize app
const app = express();
const server = http.createServer(app);
const io = new Server(server);
// Serve the static HTML file
app.use(express.static(__dirname + '/public'));
// Redis clients (with new Redis v4 syntax)
const redisClient = createClient();
const pubClient = createClient();
const subClient = createClient();
// Connect Redis clients
(async () => {
await redisClient.connect();
await pubClient.connect();
await subClient.connect();
})();
// Subscribe to Redis Pub/Sub once (to avoid multiple subscriptions)
subClient.subscribe('chat', (message, channel) => {
const data = JSON.parse(message);
io.emit('receiveMessage', data);
});
// Handle client connection
io.on('connection', (socket) => {
console.log(`User connected: ${socket.id}`);
// Store user presence in Redis (use hSet for setting hash field)
redisClient.hSet('onlineUsers', socket.id, 'online');
// Listen for messages sent by users
socket.on('sendMessage', (data) => {
pubClient.publish('chat', JSON.stringify(data)); // Publish the message via Redis Pub/Sub
});
// Handle user disconnect
socket.on('disconnect', () => {
console.log(`User disconnected: ${socket.id}`);
redisClient.hDel('onlineUsers', socket.id); // Remove user from the online users list
});
});
// Start the server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});