From 96a99d5525135668b37d88f807c8c6f0a929f94f Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 7 Jan 2023 01:41:34 +0100 Subject: [PATCH 1/1] feat: Server sends real Server-Sent-Events --- .../chat/backend/api/ChatBackendController.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java index 4aaf4f57..4ce98b2c 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java @@ -5,6 +5,7 @@ import de.juplo.kafka.chat.backend.domain.Chatroom; import de.juplo.kafka.chat.backend.persistence.StorageStrategy; import lombok.RequiredArgsConstructor; import org.springframework.http.MediaType; +import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -98,10 +99,8 @@ public class ChatBackendController .map(message -> MessageTo.from(message)); } - @GetMapping( - path = "listen/{chatroomId}", - produces = MediaType.TEXT_EVENT_STREAM_VALUE) - public Flux listen(@PathVariable UUID chatroomId) + @GetMapping(path = "listen/{chatroomId}") + public Flux> listen(@PathVariable UUID chatroomId) { return chatHome .getChatroom(chatroomId) @@ -109,12 +108,18 @@ public class ChatBackendController .orElseThrow(() -> new UnknownChatroomException(chatroomId)); } - private Flux listen(Chatroom chatroom) + private Flux> listen(Chatroom chatroom) { return chatroom .listen() .log() - .map(message -> MessageTo.from(message)); + .map(message -> MessageTo.from(message)) + .map(messageTo -> + ServerSentEvent + .builder(messageTo) + .id(messageTo.getSerial().toString()) + .event("message") + .build()); } @PostMapping("/store") -- 2.20.1