rexxars / sse-channel

Server-Sent Events "channel" where all messages are broadcasted to all connected clients, history is maintained automatically and server attempts to keep clients alive by sending "keep-alive" packets automatically.
MIT License
111 stars 11 forks source link

historical events on open? #9

Closed mark-hahn closed 8 years ago

mark-hahn commented 8 years ago

I've just gotten sse working, see #8. I'm now finding that if the server sends an event with no one listening, then when a client does connect it gets no message event. Shouldn't it get the old event from history?

rexxars commented 8 years ago

Not by default. The protocol says it should send a Last-Event-ID header if the eventsource was disconnected because of an error or similar. You can however "trick" it into sending old events by utilizing the lastEventId query parameter that some of the browser polyfills are using. Basically:

var es = new EventSource('http://host:port/channel?lastEventId=0');
es.addEventListener('message', function(e) {
    console.log(e);
});
mark-hahn commented 8 years ago

Thx.

IMHO the spec is illogical. I can't use the query trick because I'm not using a polyfill. But I can work around it by sending history whenever a new connection is made. Duplicate sends don't matter.

On Wed, Dec 30, 2015 at 1:57 PM, Espen Hovlandsdal <notifications@github.com

wrote:

Closed #9 https://github.com/rexxars/sse-channel/issues/9.

— Reply to this email directly or view it on GitHub https://github.com/rexxars/sse-channel/issues/9#event-503082743.

rexxars commented 8 years ago

Yes, I dislike the fact that you can't (spec-wise) specify how the history is handled. You CAN however use the trick without a polyfill, it's the server that implements it and allows explicitly setting a "last received event ID". When it finds one in the query string, it'll send all the events since that ID.

mark-hahn commented 8 years ago

allows explicitly setting a "last received event ID".

I can do this in the open request? That's the only place I use a URL. I'll try it.

On Wed, Dec 30, 2015 at 2:06 PM, Espen Hovlandsdal <notifications@github.com

wrote:

Yes, I dislike the fact that you can't (spec-wise) specify how the history is handled. You CAN however use the trick without a polyfill, it's the server that implements it and allows explicitly setting a "last received event ID". When it finds one in the query string, it'll send all the events since that ID.

— Reply to this email directly or view it on GitHub https://github.com/rexxars/sse-channel/issues/9#issuecomment-168081694.

GoChartingAdmin commented 7 years ago

Please share the complete code when a new client gets connected, how can we send the history etc.