fetch-event-source
fetch-event-source copied to clipboard
How can I tell when its done?
I need to do something when the stream is complete? onclose doesnt seem to be firing
any help appreciated
When the stream completes is determined by the server, and each piece of the complete stream follows through \n\n
To go into more detail, it really depends on the server implementation. Server sent events typically are implemented as a never ending stream so onclose will never fire unless the connection is closed by the client or the server.
To get around this many apis will send a "done" or "finished" event to notify the client that no more messages will be sent. For example it might look like this:
data: 1
data: 2
data: 3
event: done
data: stream is finished
So in that case you would add a listener to that done event to know when to close the connection.
Another example: This is what a stream from ChatGPT looks like. Notice the [DONE] event at the end.