Skip to content

Commit f3831b1

Browse files
Add queue checking events and keep jobs running after client disconnection
1 parent c807af8 commit f3831b1

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

backend-nestjs/src/socket/socket.gateway.ts

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ export class SocketGateway
130130

131131
// Send the updated list of users to all clients
132132
this.server.emit('users', this.socketService.getUsers());
133+
134+
// Notify the client with the queue sizes
135+
client.emit('sharedQueue', {
136+
size: this.sharedQueue.size(),
137+
});
138+
client.emit('priorityQueue', {
139+
size: this.priorityQueue.size(),
140+
});
133141
}
134142

135143
/**
@@ -143,13 +151,68 @@ export class SocketGateway
143151
const userId: string = this.socketService.removeUser(client.id);
144152
if (!userId) return;
145153

146-
// Remove the client from the queue
147-
this.removeFromQueue(client);
148-
149154
// Send the updated list of users to all clients
150155
this.server.emit('users', this.socketService.getUsers());
151156
}
152157

158+
/**
159+
* Event called when a client checks the priority queue
160+
*
161+
* @SubscribeMessage 'checkPriorityQueue'
162+
* @param {Socket} client - The client socket
163+
* @returns {Promise<void>}
164+
*/
165+
@SubscribeMessage('checkPriorityQueue')
166+
public async checkPriorityQueue(
167+
@ConnectedSocket() client: Socket,
168+
): Promise<void> {
169+
// Get the user ID from the client
170+
const userId: string = this.socketService.getUserId(client.id);
171+
if (!userId) return;
172+
173+
const inQueuePosition: number = this.priorityQueue
174+
.getItems()
175+
.findIndex((item: QueueMember) => item.userId === userId);
176+
177+
const isUserInQueue: boolean = inQueuePosition !== -1;
178+
179+
// Notify the client with their position in the queue if they are in the queue
180+
client.emit('priorityQueueStatus', {
181+
status: 'checked',
182+
userId,
183+
position: isUserInQueue ? inQueuePosition + 1 : null,
184+
});
185+
}
186+
187+
/**
188+
* Event called when a client checks the shared queue
189+
*
190+
* @SubscribeMessage 'checkSharedQueue'
191+
* @param {Socket} client - The client socket
192+
* @returns {Promise<void>}
193+
*/
194+
@SubscribeMessage('checkSharedQueue')
195+
public async checkSharedQueue(
196+
@ConnectedSocket() client: Socket,
197+
): Promise<void> {
198+
// Get the user ID from the client
199+
const userId: string = this.socketService.getUserId(client.id);
200+
if (!userId) return;
201+
202+
const inQueuePosition: number = this.sharedQueue
203+
.getItems()
204+
.findIndex((item: QueueMember) => item.userId === userId);
205+
206+
const isUserInQueue: boolean = inQueuePosition !== -1;
207+
208+
// Notify the client with their position in the queue if they are in the queue
209+
client.emit('sharedQueueStatus', {
210+
status: 'checked',
211+
userId,
212+
position: isUserInQueue ? inQueuePosition + 1 : -1,
213+
});
214+
}
215+
153216
/**
154217
* Event called when a client joins the shared queue
155218
*

0 commit comments

Comments
 (0)