Closed BearToCode closed 6 months ago
Hello @BearToCode , First of all thank you for the issue and buona festa del lavoratore.
This is indeed a bug, please update to version 0.12.2
to get the fix, then your example will start working.
I would also add that what you're trying to do manually with abstraction.js
is already done automatically by the library.
All your component code can be reduced to this
<!-- +Component.svelte -->
<script>
import { source } from 'sveltekit-sse';
const time = source('/sse').select('message');
</script>
{$time}
By default when all subscribers have unsubscribed the connection closes automatically, it leverages svelte's primitive store behavior (which can detect just that: the moment when the store has no more subscribers).\ This also takes into account subscribers across multiple components, so you don't run the risk of closing a connection that some other component is still using.
Here's a repository showcasing your example, but reduced to just using svelte store primitives https://github.com/tncrazvan/sveltekit-sse-issue-41
Let me know if this is addresses your issue.
After updating to latest version the following is working as I wanted to:
<script>
// ...
const stream = source('/sse');
stream.select('message').subscribe(console.log);
onDestroy(() => {
stream.close();
});
</script>
I still have to use the while(get(lock))
server-side.
Thanks a lot for the prompt and detailed response, and for the effort in quickly fixing the library.
Hello again,
I'm trying to connect/disconnect to SSE inside
onMount
andonDestroy
lifecycle callbacks, but it seems surprisingly hard to do. This is what I managed to do so far (demo here). I've adapted some code from #30.The stream does not reconnect after closing the first time. Also, I noticed that using
while(true)
in the server side events, the connection stays open indefinitely, so I usedwhile(get(lock))
instead.