Skip to content

Commit

Permalink
send the latest message for fast retrieval upon subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
NourAlharithi committed Nov 7, 2023
1 parent 3a6ff7c commit 3e112f4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/wsConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Set<WebSocket>>();
const subscribedChannels = new Set<string>();
Expand All @@ -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) => {
Expand Down Expand Up @@ -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': {
Expand Down

0 comments on commit 3e112f4

Please sign in to comment.