import { OnGatewayConnection , OnGatewayDisconnect , WebSocketGateway , WebSocketServer , } from '@nestjs/websockets' ; import config from 'config' ; import io from 'socket.io' ; import { LoggerService } from '../logger/logger.service' ; import { IClientQuery } from './socket.interface' ; const appSettings = config . get < IAppSettings > ( 'APP_SETTINGS' ) ; @ WebSocketGateway ( appSettings . socketPort , { pingInterval : appSettings . socketPingInterval , pingTimeout : appSettings . socketPinkTimeout , path : appSettings . socketIoPath , } ) export class SocketGateway implements OnGatewayConnection , OnGatewayDisconnect { @ WebSocketServer ( ) public server : io . Server ; constructor ( private readonly logger ) { } private getClientQuery ( client ) { return client . handshake . query as unknown as IClientQuery ; } public async handleConnection ( client ) { const { user_id } = this . getClientQuery ( client ) ; this . logger . info ( `${ user_id } - handleConnection` ) ; return this . server . emit ( 'event' , { connected : user_id } ) ; } public async handleDisconnect ( client ) { const { user_id } = this . getClientQuery ( client ) ; this . logger . info ( `${ user_id } - handleDisconnect` ) ; return this . server . emit ( 'event' , { disconnected : user_id } ) ; } }