MatthewWid / better-sse

⬆ Dead simple, dependency-less, spec-compliant server-sent events implementation for Node, written in TypeScript.
https://matthewwid.github.io/better-sse/
MIT License
558 stars 14 forks source link

Cannot push data to a non-active session. #69

Open punkpeye opened 2 months ago

punkpeye commented 2 months ago

I am debugging an odd case where if I have multiple SSE sessions, one of them inevitably starts failing with an error:

SseError: better-sse: Cannot push data to a non-active session.

Any clue how could I be getting in this situation and how to prevent it?

MatthewWid commented 2 months ago

This error means you are attempting to call the Session#push method (or calling it indirectly via stream or iterate) after it has been disconnected from the client.

Can you elaborate on what you mean by "multiple SSE sessions"? This is hard to debug without a code sample.

If you are storing multiple sessions somewhere outside of your route handler and then pushing to them directly, you might want to consider using channels instead as they prevent you from being able to push events to disconnected sessions.

In the mean time, you can add a check that the Session#isConnected property is true before you attempt to push to it.