From 307655371db6ac47a877708585aea7ed78616ab9 Mon Sep 17 00:00:00 2001 From: Mathis HERRIOT <197931332+0x485254@users.noreply.github.com> Date: Thu, 29 Jan 2026 15:54:39 +0100 Subject: [PATCH] feat: add content room subscription and messaging support - Added `join_content` and `leave_content` WebSocket events for subscribing and unsubscribing to content rooms. - Implemented `sendToContent` utility method for broadcasting messages to specific content rooms. - Enhanced connection handling with logging and session validation updates. --- backend/src/realtime/events.gateway.ts | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/backend/src/realtime/events.gateway.ts b/backend/src/realtime/events.gateway.ts index 2e6ab86..77c3c7d 100644 --- a/backend/src/realtime/events.gateway.ts +++ b/backend/src/realtime/events.gateway.ts @@ -1,9 +1,12 @@ import { Logger } from "@nestjs/common"; import { ConfigService } from "@nestjs/config"; import { + ConnectedSocket, + MessageBody, OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit, + SubscribeMessage, WebSocketGateway, WebSocketServer, } from "@nestjs/websockets"; @@ -54,6 +57,8 @@ export class EventsGateway if (!session.accessToken) { this.logger.warn(`Client ${client.id} unauthorized connection`); + // Permettre les connexions anonymes pour voir les commentaires en temps réel ? + // Pour l'instant on déconnecte car le système actuel semble exiger l'auth client.disconnect(); return; } @@ -75,8 +80,30 @@ export class EventsGateway this.logger.log(`Client disconnected: ${client.id}`); } + @SubscribeMessage("join_content") + handleJoinContent( + @ConnectedSocket() client: Socket, + @MessageBody() contentId: string, + ) { + client.join(`content:${contentId}`); + this.logger.log(`Client ${client.id} joined content room: ${contentId}`); + } + + @SubscribeMessage("leave_content") + handleLeaveContent( + @ConnectedSocket() client: Socket, + @MessageBody() contentId: string, + ) { + client.leave(`content:${contentId}`); + this.logger.log(`Client ${client.id} left content room: ${contentId}`); + } + // Méthode utilitaire pour envoyer des messages à un utilisateur spécifique sendToUser(userId: string, event: string, data: any) { this.server.to(`user:${userId}`).emit(event, data); } + + sendToContent(contentId: string, event: string, data: any) { + this.server.to(`content:${contentId}`).emit(event, data); + } }