From 3e112f4cafb590982e3463e78bfc08bca47ffd28 Mon Sep 17 00:00:00 2001 From: Nour Alharithi Date: Tue, 7 Nov 2023 10:22:35 -0800 Subject: [PATCH] send the latest message for fast retrieval upon subscribe --- src/wsConnectionManager.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/wsConnectionManager.ts b/src/wsConnectionManager.ts index eea8707..abf798f 100644 --- a/src/wsConnectionManager.ts +++ b/src/wsConnectionManager.ts @@ -24,7 +24,10 @@ const REDIS_PASSWORD = process.env.REDIS_PASSWORD; async function main() { const redisClient = new RedisClient(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD); + const lastMessageRetriever = new RedisClient(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD); + await redisClient.connect(); + await lastMessageRetriever.connect(); const channelSubscribers = new Map>(); const subscribedChannels = new Set(); @@ -34,6 +37,9 @@ async function main() { subscribers.forEach((ws) => { ws.send(JSON.stringify({ channel: subscribedChannel, data: message })); }); + + // Save and persist last message + lastMessageRetriever.client.set(`last_update_${subscribedChannel}`, message); }); wss.on('connection', (ws: WebSocket) => { @@ -68,6 +74,13 @@ async function main() { channelSubscribers.set(channel, subscribers); } channelSubscribers.get(channel).add(ws); + + // Fetch and send last message + const lastMessage = await lastMessageRetriever.client.get(`last_update_${channel}`); + if (lastMessage !== null) { + console.log('sending last message on new subscribe'); + ws.send(JSON.stringify({ channel, data: lastMessage })); + } break; } case 'unsubscribe': {