fetch-event-source icon indicating copy to clipboard operation
fetch-event-source copied to clipboard

How can I tell when its done?

Open PowellT opened this issue 1 year ago • 3 comments

I need to do something when the stream is complete? onclose doesnt seem to be firing

any help appreciated

PowellT avatar Aug 15 '24 19:08 PowellT

When the stream completes is determined by the server, and each piece of the complete stream follows through \n\n

a554878526 avatar Nov 26 '24 15:11 a554878526

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.

joshmossas avatar Dec 11 '24 19:12 joshmossas

Another example: This is what a stream from ChatGPT looks like. Notice the [DONE] event at the end. Image

joshmossas avatar Dec 11 '24 19:12 joshmossas