I have a uses case in which I need to send push notifications to the Android or IOS client. The notification event should be unicast. Each message is relevant for a single client only.
How can I achieve that? I have previously broadcast events to multiple clients using code like below. I want to send a notification to an event particular subscriber for which event belongs over SSE.
@GetMapping("/sse-emitter")
public SseEmitter sseEmitter() {
SseEmitter emitter = new SseEmitter();
Executors.newSingleThreadExecutor().execute(() -> {
try {
for (int i = 0; true; i++) {
SseEmitter.SseEventBuilder event = SseEmitter.event()
.id(String.valueOf(i))
.name("SSE_EMITTER_EVENT")
.data("SSE EMITTER - " + LocalTime.now().toString());
emitter.send(event);
Thread.sleep(1000);
}
} catch (Exception ex) {
emitter.completeWithError(ex);
}
});
return emitter;
}
P.S I am using this approach to keep map of SSEEmitters.
SSE Emitter : Manage timeouts and complete()
I will test it properly and update here