Files
lcbp3/backend/src/modules/notification/notification.gateway.ts
2025-11-23 02:23:38 +07:00

39 lines
961 B
TypeScript

import {
WebSocketGateway,
WebSocketServer,
OnGatewayConnection,
OnGatewayDisconnect,
} from '@nestjs/websockets';
import { Server, Socket } from 'socket.io';
import { Logger } from '@nestjs/common';
@WebSocketGateway({
cors: {
origin: '*',
},
namespace: 'notifications',
})
export class NotificationGateway
implements OnGatewayConnection, OnGatewayDisconnect
{
@WebSocketServer()
server!: Server; // ✅ FIX: เติม ! (Definite Assignment Assertion)
private readonly logger = new Logger(NotificationGateway.name);
handleConnection(client: Socket) {
this.logger.log(`Client connected: ${client.id}`);
}
handleDisconnect(client: Socket) {
this.logger.log(`Client disconnected: ${client.id}`);
}
/**
* ส่งแจ้งเตือนไปหา User แบบ Real-time
*/
sendToUser(userId: number, payload: any) {
this.server.to(`user_${userId}`).emit('new_notification', payload);
}
}