-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.js
More file actions
74 lines (58 loc) · 2.11 KB
/
websocket.js
File metadata and controls
74 lines (58 loc) · 2.11 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// websocket.js
const WebSocket = require('ws');
const Redis = require('ioredis');
let wss;
function initWebSocketServer(server) {
wss = new WebSocket.Server({ server });
// Create separate Redis clients for Pub/Sub
const pubClient = new Redis(process.env.REDIS_URL || 'redis://localhost:6379');
const subClient = new Redis(process.env.REDIS_URL || 'redis://localhost:6379');
// Subscribe to the Redis channel for chat messages
subClient.subscribe('chat_messages', (err, count) => {
if (err) {
console.error('Failed to subscribe to Redis channel:', err);
} else {
console.log(`Subscribed to ${count} Redis channel(s).`);
}
});
// Handle incoming messages from Redis
subClient.on('message', (channel, message) => {
const parsedMessage = JSON.parse(message);
const { threadId, data } = parsedMessage;
// Broadcast the message to all clients connected to the same threadId
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.threadId === threadId) {
client.send(JSON.stringify(data));
}
});
});
wss.on('connection', (ws, req) => {
const url = req.url;
const threadId = url.split('/').pop();
console.log(`WebSocket connected for thread: ${threadId}`);
ws.threadId = threadId;
ws.on('message', (message) => {
console.log(`Received message for thread ${threadId}:`, message);
// Publish the message to Redis
const messagePayload = JSON.stringify({
threadId,
data: JSON.parse(message), // Assuming message is a JSON string
});
pubClient.publish('chat_messages', messagePayload);
});
ws.on('close', () => {
console.log(`Connection closed for thread: ${threadId}`);
});
});
}
function getWebSocketServer() {
return wss;
}
function broadcastMessage(threadId, messageData) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.threadId === String(threadId)) {
client.send(JSON.stringify(messageData));
}
});
}
module.exports = { initWebSocketServer, getWebSocketServer, broadcastMessage };