Skip to content

Commit

Permalink
fix: allow user to instantly queue after refreshing while in queue
Browse files Browse the repository at this point in the history
  • Loading branch information
shishirbychapur committed Nov 12, 2024
1 parent 8929f9b commit 9be2ba5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
13 changes: 5 additions & 8 deletions backend/matching-service/src/services/ws.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import url from 'url'
import WebSocket, { Server as WebSocketServer } from 'ws'
import loggerUtil from '../common/logger.util'
import { addUserToMatchmaking, removeUserFromMatchingQueue } from '../controllers/matching.controller'
import mqConnection from './rabbitmq.service'

export class WebSocketConnection {
private wss: WebSocketServer
Expand All @@ -14,6 +15,7 @@ export class WebSocketConnection {
this.wss.on('connection', (ws: WebSocket, req: IncomingMessage) => {
const query = url.parse(req.url, true).query
const websocketId = query.id as string
const userId = query.userId as string

if (!websocketId) {
ws.close(1008, 'Missing userId')
Expand All @@ -22,15 +24,9 @@ export class WebSocketConnection {

this.clients.set(websocketId, ws)

// // Close connection after 2 minutes automatically
// const closeAfterTimeout = setTimeout(() => {
// ws.close(1000, 'Connection closed by server after 2 minutes')
// }, 120000)

ws.on('message', (message: string) => this.handleMessage(message, websocketId))
ws.on('close', () => {
// clearTimeout(closeAfterTimeout)
this.handleClose(websocketId)
this.handleClose(websocketId, userId)
})
})
}
Expand Down Expand Up @@ -61,9 +57,10 @@ export class WebSocketConnection {
}

// Handle WebSocket close event
private handleClose(websocketId: string): void {
private handleClose(websocketId: string, userId: string): void {
loggerUtil.info(`User ${websocketId} disconnected`)
this.clients.delete(websocketId)
mqConnection.cancelUser(websocketId, userId)
}

// Send a message to a specific user by websocketId
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/dashboard/new-session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const NewSession = () => {

// Refactor this
const wsUrl = (process.env.NEXT_PUBLIC_API_URL || 'ws://localhost:3006')?.concat(
`/api/matching/ws/?id=${websocketId}`
`/api/matching/ws/?id=${websocketId}&userId=${session?.user.id ?? ''}`
)
const socket = new WebSocket(wsUrl)
setTimeout(() => {
Expand Down

0 comments on commit 9be2ba5

Please sign in to comment.