Skip to content

Commit

Permalink
fix: heartbeat now keeps room alive
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumm committed Jan 28, 2024
1 parent 50d8c10 commit 623a5ee
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/server/room-state/room-state.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,19 @@ export async function setRoomState({
roomState.isFlipAction = false;
}

// If the room state has changed, we update Redis and publish to it's WebSocket channel
if (roomState.hasChanged) {
// If the room state has changed or if the room state hasn't changed in the last 4 minutes
// we update the room state in Redis and publish to the WebSocket channel
if (
roomState.hasChanged ||
roomState.lastUpdated < Date.now() - 1000 * 60 * 4
) {
roomState.lastUpdated = Date.now();
promises.push(
redis.set(`room:${roomId}`, roomState, { ex: 60 * 5 }),
publishWebSocketEvent({ roomState, userId }),
);
}

// If the room state hasn't changed in the last 4 minutes, we extend the expiration time
// TODO: close the room after 5 minutes of inactivity
if (
!roomState.hasChanged &&
roomState.lastUpdated > Date.now() - 1000 * 60 * 4
) {
promises.push(redis.expire(`room:${roomId}`, 60 * 5));
}

await Promise.allSettled(promises).then((results) => {
for (const result of results) {
if (result.status === 'rejected' && result.reason instanceof TRPCError) {
Expand Down

0 comments on commit 623a5ee

Please sign in to comment.