From df4992876c58debe558df9277b28c3087b6467e2 Mon Sep 17 00:00:00 2001 From: Maybeiley <2784519@gmail.com> Date: Sun, 23 Feb 2025 16:46:54 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20#265=20SSE=20=EC=97=B0=EA=B2=B0=20?= =?UTF-8?q?=EB=81=8A=EA=B9=80=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/notification/notification.controller.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/modules/notification/notification.controller.ts b/src/modules/notification/notification.controller.ts index 2dba60a..fe1d378 100644 --- a/src/modules/notification/notification.controller.ts +++ b/src/modules/notification/notification.controller.ts @@ -1,8 +1,8 @@ import { Controller, Get, Param, Patch, Post, Sse } from '@nestjs/common'; import NotificationService from './notification.service'; import { UserId } from 'src/common/decorators/user.decorator'; -import { map, Observable } from 'rxjs'; -import { Public } from 'src/common/decorators/public.decorator'; +import { interval, merge, Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; import { NotificationEventName, NotificationProperties } from './types/notification.types'; @Controller('notifications') @@ -41,10 +41,16 @@ export default class NotificationController { @Sse('stream') stream(@UserId() userId: string): Observable<{ data: string }> { console.log(`SSE connection for userId: ${userId}`); - return this.service.stream(userId).pipe( + + // 40초마다 Heartbeat 전송하여 연결 유지 + const heartbeat$ = interval(40000).pipe(map(() => ({ data: 'ping' }))); + const notification$ = this.service.stream(userId).pipe( map((content: string) => { return { data: content }; }) ); + + // Heartbeat와 알림 stream 병합 + return merge(heartbeat$, notification$); } }