Open
Conversation
added 10 commits
October 28, 2025 10:19
joonfluence
reviewed
Nov 12, 2025
| import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
|
||
| @Configuration | ||
| @EnableWebSocketMessageBroker |
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+9
to
+19
| @Controller | ||
| @RequiredArgsConstructor | ||
| public class MessageWebSocketController { | ||
|
|
||
| private final SimpMessagingTemplate messagingTemplate; | ||
|
|
||
| @MessageMapping("/messages") | ||
| public void sendMessage(MessageCreateRequest request) { | ||
| String destination = "/sub/channels/" + request.channelId() + ".messages"; | ||
| messagingTemplate.convertAndSend(destination, request.content()); | ||
| } |
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+11
to
+21
| @Component | ||
| @RequiredArgsConstructor | ||
| public class WebSocketRequiredEventListener { | ||
| private final SimpMessagingTemplate simpMessagingTemplate; | ||
|
|
||
| @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| public void handleWebSocketMessage(MessageCreatedEvent event) { | ||
| String destination = "/sub/channels/" + event.channelId() + ".messages"; | ||
| simpMessagingTemplate.convertAndSend(destination, event.content()); | ||
| } | ||
| } |
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+90
to
+91
| eventPublisher.publishEvent( | ||
| new MessageCreatedEvent(message.getId(), channelId, author.getId(), message.getContent()) |
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+16
to
+18
| private final ConcurrentLinkedDeque<UUID> eventIdQueue = new ConcurrentLinkedDeque<>(); | ||
| // 실제 이벤트 데이터를 저장(eventId -> message) | ||
| private final Map<UUID, SseMessage> messages = new ConcurrentHashMap<>(); |
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+19
to
+23
| @Service | ||
| @RequiredArgsConstructor | ||
| public class SseService { | ||
| private final SseEmitterRepository sseEmitterRepository; | ||
| private final SseMessageRepository sseMessageRepository; |
joonfluence
reviewed
Nov 12, 2025
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+99
to
+116
| // Ping 테스트 후 끊긴 연결을 제거 | ||
| @Scheduled(fixedDelay = 1000 * 60 * 30) | ||
| public void cleanUp() { | ||
| for (UUID receiverId : sseEmitterRepository.findAllIds()) { | ||
| List<SseEmitter> emitters = sseEmitterRepository.findAllById(receiverId); | ||
| List<SseEmitter> deadEmitters = new ArrayList<>(); | ||
|
|
||
| for (SseEmitter emitter : emitters) { | ||
| boolean alive = ping(emitter); | ||
| if (!alive) { | ||
| deadEmitters.add(emitter); | ||
| } | ||
| } | ||
| for (SseEmitter emitter : deadEmitters) { | ||
| sseEmitterRepository.remove(receiverId, emitter); | ||
| } | ||
| } | ||
| } |
joonfluence
reviewed
Nov 12, 2025
Comment on lines
+11
to
+28
| public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
|
||
| @Override | ||
| public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
| // 메세지 구독 경로 prefix | ||
| registry.enableSimpleBroker("/sub"); | ||
|
|
||
| // 메세지 발행 경로 prefix | ||
| registry.setApplicationDestinationPrefixes("/pub"); | ||
| } | ||
|
|
||
| @Override | ||
| public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
| registry.addEndpoint("/ws") | ||
| .setAllowedOrigins("*") | ||
| .withSockJS(); | ||
| } | ||
| } |
Collaborator
There was a problem hiding this comment.
WebSocketMessageBrokerConfigurer 통해 HTTP 요청 간 JWT 인증 수행
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
요구사항
기본
웹소켓 구현하기
SSE 구현하기
SSE 환경을 구성하세요.
connect: SseEmitter 객체를 생성합니다.
send, broadcast: SseEmitter 객체를 통해 이벤트를 전송합니다.
cleanUp: 주기적으로 ping을 보내서 만료된 SseEmitter 객체를 삭제합니다.
ping: 최초 연결 또는 만료 여부를 확인하기 위한 용도로 더미 이벤트를 보냅니다.
각 메시지 별로 고유한 ID를 부여합니다.
클라이언트에서 LastEventId를 전송해 이벤트 유실 복원이 가능하도록 해야 합니다.
기존에 클라이언트에서 폴링 방식으로 주기적으로 요청하던 데이터를 SSE를 이용해 서버에서 실시간으로 전달하는 방식으로 리팩토링하세요.
새 알림이 생성되었을 때 클라이언트에 이벤트를 전송하세요.
클라이언트는 이 이벤트를 수신하면 알림 목록에 알림을 추가합니다.
이벤트 명세
배포 아키텍처 구성하기
심화
주요 변경사항
스크린샷
멘토에게