mpetrunic / fastify-sse-v2

Provide Server-Sent Events to Fastify
MIT License
96 stars 19 forks source link

Reconnect to the same stream if connection is lost #85

Closed evilmastermind closed 1 month ago

evilmastermind commented 1 month ago

I'm using azure/fetchEventSource on the frontend to connect to fastify-sse-v2 and establish a SSE connection.

I just found out that, when an SSE connection is open and I minimize the browser, and reopen it again, the browser (or fetchEventSource) establish a new SSE connection with the backend, which create a new one in fastify instead of resuming the original one.

Is this a common problem of SSE, a limitation of the plugin, or a misconfiguration on my side? How do you handle this scenario? I tried to send, from the frontend, the "Last-Event-ID" header, with the id of the last event from fastify, on connect, but that didn't seem to work.

Thank you in advance for your help

evilmastermind commented 1 month ago

Sorry, I should have better read fetchEventSource's documentation. There is actually a flag called openWhenHidden which prevents the plugin from resetting the connection when the document is hidden. That solved my problem