Reputation: 123
Is it possible to set the Last-Event-Id header of an EventSource ? I have a simple chat app that keeps the messages cached. When I connect, I send all the chat since the Last-Event-Id, or all of them if it is not provided.
Since I keep the messages, I figured I might be able to pass the Id to the EventSource constructor to avoid it giving me back all the messages I already have. Is that simply not possible ?
Upvotes: 3
Views: 3308
Reputation: 13135
The EventSource request will only have the Last-Event-Id
header if the connection breaks and the client will have to reconnect. You can "force" this reconnection by letting the server break the first connection.
It goes like this:
Last-Event-Id
is a request header.
Last-Event-Id
is present:
Last-Event-Id
is not present:
Last-Event-Id
header, and now you jump up to number 2.The only problem with this approach is the delay caused by the client because it has to make two connections. I haven't tested the delay, but it looks like something around 3-5 seconds between each request.
Upvotes: 1
Reputation: 7066
You can pass information on the query string.
var source = EventSource("source?eventId=12345");
Ultimately it is just up to you to make sense of it on the server side and return the correct event(s).
Upvotes: 2