0

I created a Server-Sent Event connection with Spring and Angular to send a periodic message about possible incoming restart.

But the SSE connection is limited to 6 connections per user. This means that if I open more than 6 tabs, everything slows down because the browser can't get connections - the page doesn't load.

angular

const source = new EventSource('/api/incoming-restart-event')
source.addEventListener('message', (message: MessageEvent) => {
  minutesUntilRestart = message.data
})

Spring

@GetMapping(path = "/incoming-restart-event", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Date> incomingRestartEventStream() {
    return Flux.interval(Duration.ofSeconds(3), Duration.ofSeconds(100))
            .map(sequence -> incomingRestartService.getIncomingRestartTime() != null
                    ? incomingRestartService.getIncomingRestartTime()
                    : new Date(0));
}

What would be the best way to only send updates to the active tab (or just last active tab)? Basically - I want to make sure that we aren't opening 6+ open connections.

I would prefer to handle this in the back-end so I don't have to build some kind of a front-end session handler for trying to count user's active tabs.

PadaKatel
  • 177
  • 2
  • 15
  • `But the SSE connection is limited to 6 connections per user` why, as this makes no sense. – Toerktumlare Aug 02 '21 at 11:16
  • I agree. But that's just how it is. https://developer.mozilla.org/en-US/docs/Web/API/EventSource https://stackoverflow.com/questions/16852690/sseeventsource-why-no-more-than-6-connections – PadaKatel Aug 02 '21 at 11:40
  • whats the user case, keeping track of 1000 clients active tabs is going to be a pain. I would find another way, good luck. – Toerktumlare Aug 02 '21 at 13:21
  • I just want to make sure that the website keeps working when the user opens more than 6 tabs. If needed and it's possible, I would be okay with a front-end solution as well - so each user's browser doesn't open new connections with each tab. – PadaKatel Aug 02 '21 at 14:01
  • This feels more like you want to implement something that doesnt seem like a problem yet. But good luck. – Toerktumlare Aug 02 '21 at 14:27
  • It actually is a problem - I have a tool that people use with multiple tabs to multitask and work with different datasets which are relevant to each other. – PadaKatel Aug 02 '21 at 15:33
  • 6 connection is only applicable if you are not using HTTP/2. Recent version of Spring supports HTTP/2 so I would recommend to upgrade your stack. – nicholasnet Aug 03 '21 at 02:07
  • Unfortunately I can't upgrade to to HTTP/2 as of this moment. I guess my only solution is to make it a regular REST end-point to query state and create a front-end poller which shoots the query on a timer. – PadaKatel Aug 03 '21 at 08:49
  • @PadaKatel did you actually find a way to get number of connections ? From the debugger i can get info like `((FluxRefCount) events).connection` and check that against null, but its functions not allowed inside the code – thahgr Aug 09 '22 at 13:07
  • No, I unfortunately never checked the number of active connections. The 6 connection limit was true and so I couldn't use webflux for this feature. – PadaKatel Aug 09 '22 at 20:43

0 Answers0