Created
March 7, 2023 19:46
-
-
Save lordnox/39691516d24d90ee5d92c8279cb18c19 to your computer and use it in GitHub Desktop.
SSE-cleanup not working in solid start
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createEffect, createSignal, onCleanup } from 'solid-js' | |
import server$, { eventStream } from 'solid-start/server' | |
function createEventStream({ url }: { url: string }, onMessage: (ev: MessageEvent) => void) { | |
createEffect(() => { | |
const eventSource = new EventSource(url) | |
eventSource.addEventListener('chat', (event) => { | |
onMessage(event) | |
}) | |
onCleanup(() => eventSource.close()) | |
}) | |
} | |
export default function Page() { | |
let [state, setState] = createSignal('test data') | |
createEventStream( | |
server$(async function () { | |
return eventStream(server$.request, (send) => { | |
send('chat', 'Hello world') | |
let x = 1 | |
const interval = setInterval(() => { | |
send('chat', 'Hi :-D ' + x++) | |
console.log(x) | |
}, 2000) | |
return () => { | |
console.log('disconnect') | |
clearInterval(interval) | |
} | |
}) | |
}), | |
(event) => { | |
setState(event.data) | |
}, | |
) | |
return <h1 id="chat">{state()}</h1> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment