From 755ddda123f01b2472543f5da6139654b77c458a Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Wed, 11 Feb 2026 04:48:15 +0530 Subject: [PATCH] feat: Implement direct notification for new messages and read receipts to conversation participants. --- src/messages/messages.gateway.ts | 27 ++++++++++++++++++++++++--- src/messages/messages.service.ts | 22 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/messages/messages.gateway.ts b/src/messages/messages.gateway.ts index 4332ec6..265015f 100644 --- a/src/messages/messages.gateway.ts +++ b/src/messages/messages.gateway.ts @@ -178,6 +178,16 @@ export class MessagesGateway implements OnGatewayConnection, OnGatewayDisconnect .to(`conversation:${data.conversationId}`) .emit('new_message', message); + // Also send directly to the other participant's socket(s) + // so they receive the message even if they haven't joined this room + const participants = await this.messagesService.getConversationParticipants(data.conversationId); + if (participants) { + const otherUserId = client.userId === participants.userId + ? participants.agentUserId + : participants.userId; + this.sendToUser(otherUserId, 'new_message', message); + } + return { success: true, message }; } catch (error) { return { error: error.message }; @@ -232,12 +242,23 @@ export class MessagesGateway implements OnGatewayConnection, OnGatewayDisconnect try { await this.messagesService.markMessagesAsRead(data.conversationId, client.userId); - // Notify the other party that messages have been read - client.to(`conversation:${data.conversationId}`).emit('messages_read', { + const readEvent = { conversationId: data.conversationId, readBy: client.userId, readAt: new Date(), - }); + }; + + // Notify via room (for clients viewing this conversation) + client.to(`conversation:${data.conversationId}`).emit('messages_read', readEvent); + + // Also notify the other participant directly + const participants = await this.messagesService.getConversationParticipants(data.conversationId); + if (participants) { + const otherUserId = client.userId === participants.userId + ? participants.agentUserId + : participants.userId; + this.sendToUser(otherUserId, 'messages_read', readEvent); + } return { success: true }; } catch (error) { diff --git a/src/messages/messages.service.ts b/src/messages/messages.service.ts index 8786f22..be9afaa 100644 --- a/src/messages/messages.service.ts +++ b/src/messages/messages.service.ts @@ -555,6 +555,28 @@ export class MessagesService { } } + /** + * Get participant user IDs for a conversation (lightweight query) + */ + async getConversationParticipants(conversationId: string): Promise<{ userId: string; agentUserId: string } | null> { + const conversation = await this.prisma.conversation.findUnique({ + where: { id: conversationId }, + select: { + userId: true, + agentProfile: { + select: { userId: true }, + }, + }, + }); + + if (!conversation) return null; + + return { + userId: conversation.userId, + agentUserId: conversation.agentProfile.userId, + }; + } + /** * Get total unread count for a user across all conversations */