简体   繁体   中英

Server sent events (SSE) - automatic reconnect

I am having trouble understanding how the "automatic reconnect" works for SSE.

I've been reading the documentation of Mozilla and WC3 and several other discussions, but still I am still not sure how it works.

https://html.spec.whatwg.org/multipage/server-sent-events.html https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events

I have implemented SSE using gorilla/mux in Golang and it all works. Now to the interesting part, I can break the connection from the server but the connection will magically be restored seconds later. I haven't implemented any functionality for this myself, so I am not sure how this happens?

All major browsers will try to automatically reconnect, however I never manually set any Event ID. Will the the browser reconnect but just not receive any lost data sent during disconnect? (this is what the use of EventID can solve, but I have no interest in it). Ie, can the browser reconnect without the use of EventID? Or could it be something else that makes this "reconnect" possible?

As per specification client must send Last-Event-Id header to tell the server about last event id it successfully received. Only when events are send with id field with empty value (different from just sending data and ommiting id completely) Last-Event-Id resets on client side (you can think of this as block transactions).

See section 5 Processing Model in https://www.w3.org/TR/2009/WD-eventsource-20090421/

If the event source's last event ID string is not the empty string, then a Last-Event-Id HTTP header must be included with the request, whose value is the value of the event source's last event ID string.

There's good explanation of this later in section 7 Interpreting an Event Stream describing on test-stream practical example on how Last-Event-Id sent by client side is affected by value (and presence) of event id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM