-
-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eventStream doesn't connect properly after browser refresh, on node server setup #263
Comments
If the app is refreshed on the browser the connection is closed and a new one will be open once the app loads again, there's no way the browser will keep the connection open. |
I phrased it wrong, it doesn't connect properly after the reload and is not in the state where it can enqueue. |
This timer based example works perfectly. But not the one where the import type { LoaderFunctionArgs } from '@remix-run/node'
import { eventStream } from 'remix-utils/sse/server'
import { interval } from 'remix-utils/timers'
export async function loader({ request }: LoaderFunctionArgs) {
return eventStream(request.signal, send => {
async function run() {
for await (const _ of interval(2000, { signal: request.signal })) {
send({ event: 'time', data: new Date().toISOString() })
}
}
run()
return () => {}
})
} |
So when the events are fired continuously with a timer, the |
If I add a clog in here: function send({ event = 'message', data }: SendFunctionArgs) {
console.log('🔴 signal aborted:', signal.aborted)
controller.enqueue(encoder.encode(`event: ${event}\n`))
controller.enqueue(encoder.encode(`data: ${data}\n\n`))
} I see the
So it changes after every event. But if I ditch the export async function loader() {
const controller = new AbortController()
controller.signal.addEventListener('abort', () => controller.abort())
return eventStream(controller.signal, send => {
function handle(state: string) {
try {
send({ event: 'reportStatus', data: state })
} catch (error: any) {
logger.error(error, 'Error at the `send()` method:')
}
}
emitter.once('active', state => {
console.log(state)
handle(state)
})
emitter.once('stalled', state => {
console.log(state)
handle(state)
})
emitter.once('completed', state => {
console.log(state)
handle(state)
return controller.abort()
})
return () => {
emitter.off('completed', handle)
emitter.off('stalled', handle)
emitter.off('active', handle)
}
})
} Wonder if that has some repercussions down the line? |
Okay I got it, nothing wrong with this lib :) The problem was I don't revalidate the route because the background job returns nothing, so the React component also wasn't re-rendering, because SSE data won't trigger revalidation and I wasn't putting my SSE data into a state. That caused the |
@hilja I was running into similar issues and found out that it was a bug in my In your example above, the same thing can be observed
The |
Describe the bug
It works good until I refresh the browser and issue a event.
Your Example Website or App
https://stackblitz.com/edit/remix-run-remix-p98wmt?file=server.ts,remix.config.js,app%2FstatusEmitter.ts,app%2Froutes%2Freport.subscribe.ts
Steps to Reproduce the Bug or Issue
NOTE: the error won't appear in StackBlitz, you need to clone the repo and run it locally https://github.com/hilja/remix-run-sse
/report/foo
pageExpected behavior
No errors, the stream should stay open.
Screenshots or Videos
No response
Platform
Additional context
I don't know if this is just a development environment thing or what, haven't deployed it to a real server yet. But I've tried to run the built project, same thing.
There was a header flush update on Remix just remix-run/remix#7619. It made it better, the stream is not in pending state at first. But didn't fix my issue.
The text was updated successfully, but these errors were encountered: