diff --git a/be/carrot/src/main/java/com/example/carrot/notification/component/SseEmitters.java b/be/carrot/src/main/java/com/example/carrot/notification/component/SseEmitters.java index ee9124aff..44011473c 100644 --- a/be/carrot/src/main/java/com/example/carrot/notification/component/SseEmitters.java +++ b/be/carrot/src/main/java/com/example/carrot/notification/component/SseEmitters.java @@ -28,18 +28,18 @@ public class SseEmitters { @Value("${sse.timeout}") private long emitterTimeout; - public void add(Long userId) { + public SseEmitter add(Long userId) { SseEmitter emitter = new SseEmitter(emitterTimeout); userSseEmitterMap.put(userId, emitter); - sendNotification(userId, EVENT_NAME_CONNECTED); + return sendNotification(userId, EVENT_NAME_CONNECTED); } public void sendNotification(Long receiverId, Notification notification) { sendNotification(receiverId, notification.toString()); } - private void sendNotification(Long receiverId, String notificationData) { + private SseEmitter sendNotification(Long receiverId, String notificationData) { SseEmitter emitter = userSseEmitterMap.get(receiverId); try { @@ -47,6 +47,8 @@ private void sendNotification(Long receiverId, String notificationData) { .id(String.valueOf(receiverId)) .name(EVENT_NAME_NOTIFICATION) .data(notificationData)); + + return emitter; } catch (IOException e) { emitter.completeWithError(new CustomException(StatusCode.SEND_SSE_EMITTER_ERROR)); } diff --git a/be/carrot/src/main/java/com/example/carrot/notification/controller/NotificationController.java b/be/carrot/src/main/java/com/example/carrot/notification/controller/NotificationController.java index 46a2ff44a..f16d9094a 100644 --- a/be/carrot/src/main/java/com/example/carrot/notification/controller/NotificationController.java +++ b/be/carrot/src/main/java/com/example/carrot/notification/controller/NotificationController.java @@ -1,11 +1,14 @@ package com.example.carrot.notification.controller; +import javax.servlet.http.HttpServletResponse; + import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import com.example.carrot.notification.service.NotificationService; @@ -19,11 +22,9 @@ public class NotificationController { private final NotificationService notificationService; @GetMapping(value = "/subscribe", produces = MediaType.TEXT_EVENT_STREAM_VALUE) - public ResponseEntity connect(@RequestAttribute Long userId) { - notificationService.subscribe(userId); - + public ResponseEntity connect(@RequestAttribute Long userId) { return ResponseEntity.ok() .header("X-Accel-Buffering", "no") - .build(); + .body(notificationService.subscribe(userId)); } } diff --git a/be/carrot/src/main/java/com/example/carrot/notification/service/NotificationService.java b/be/carrot/src/main/java/com/example/carrot/notification/service/NotificationService.java index 982572c38..79592eb4b 100644 --- a/be/carrot/src/main/java/com/example/carrot/notification/service/NotificationService.java +++ b/be/carrot/src/main/java/com/example/carrot/notification/service/NotificationService.java @@ -1,6 +1,7 @@ package com.example.carrot.notification.service; import org.springframework.stereotype.Service; +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import com.example.carrot.notification.component.SseEmitters; import com.example.carrot.notification.entity.Notification; @@ -16,8 +17,8 @@ public class NotificationService { private final UserRepository userRepository; private final SseEmitters sseEmitters; - public void subscribe(Long userId) { - sseEmitters.add(userId); + public SseEmitter subscribe(Long userId) { + return sseEmitters.add(userId); } public void send(User receiver, Notification notification) {