-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredisSubscriber.js
More file actions
40 lines (35 loc) · 1.33 KB
/
redisSubscriber.js
File metadata and controls
40 lines (35 loc) · 1.33 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
// website/redisSubscriber.js
const redisClient = require('./config/redisClient');
const { userSockets, vizSockets } = require('./sockets/websocket');
async function setupRedisSubscriber() {
const subscriber = redisClient.duplicate();
await subscriber.connect();
// Existing: real-time events -> specific user socket
await subscriber.subscribe('detected_events', (message) => {
try {
const data = JSON.parse(message);
const ws = userSockets.get(data.userId);
if (ws && ws.readyState === ws.OPEN) {
ws.send(JSON.stringify(data));
console.log(`📤 Real-time event sent to ${data.userId}`);
}
} catch (err) {
console.error('❌ Failed to process Redis Pub/Sub message:', err);
}
});
console.log('✅ Subscribed to Redis "detected_events" channel');
// NEW: viz frames -> broadcast to all viz viewers
await subscriber.subscribe('viz_frames', (message) => {
// message is a JSON string: { type:'viz_frame', image:'data:image/jpeg;base64,...', ts:... }
for (const ws of vizSockets) {
try {
if (ws.readyState === ws.OPEN) ws.send(message);
} catch (err) {
console.error('❌ Failed to send viz frame:', err);
}
}
});
console.log('✅ Subscribed to Redis "viz_frames" channel');
return subscriber;
}
module.exports = setupRedisSubscriber;