Skip to content

Commit

Permalink
resub on redis reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
NourAlharithi committed Nov 10, 2023
1 parent 65e575a commit 384e785
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/wsConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ async function main() {
const channelSubscribers = new Map<string, Set<WebSocket>>();
const subscribedChannels = new Set<string>();

redisClient.client.on('connect', () => {
subscribedChannels.forEach(async (channel) => {
try {
await redisClient.client.subscribe(channel);
} catch (error) {
console.error(`Error subscribing to ${channel}:`, error);
}
});
});

redisClient.client.on('message', (subscribedChannel, message) => {
const subscribers = channelSubscribers.get(subscribedChannel);
subscribers.forEach((ws) => {
Expand Down Expand Up @@ -132,7 +142,6 @@ async function main() {

// Listen for pong messages
ws.on('pong', () => {
console.log('poooooooong');
isAlive = true;
clearTimeout(pongTimeoutId);
});
Expand All @@ -146,6 +155,7 @@ async function main() {
if (subscribers.delete(ws) && subscribers.size === 0) {
redisClient.client.unsubscribe(channel);
channelSubscribers.delete(channel);
subscribedChannels.delete(channel);
}
});
});
Expand Down

0 comments on commit 384e785

Please sign in to comment.