Lib.AspNetCore.ServerSentEvents
Lib.AspNetCore.ServerSentEvents copied to clipboard
Controlling Format of Keep Alive
My frontend guys aren't happy with 'KEEPALIVE', is it possible to change the format without having to implement my own keepalive mechanism?
Hi @ssteiner,
Before we talk about actual feasibility of changing the format, I would like to understand a little bit deeper your need. Currently keep alive is being send as a comment.
: KEEPALIVE
According to Server Sent Events specification around interpreting an event stream, comments should simply be ignored (https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation).
If the line starts with a U+003A COLON character (:) Ignore the line.
So I would really like to understand what is the need here, as this is something which shouldn't require any handing or concern on the fronted side.
That said, if the only need would be to provide ability to use different comment value, I could expose it through options. This would allow for something like below.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddServerSentEvents<INotificationsServerSentEventsService, NotificationsServerSentEventsService>(options =>
{
options.KeepaliveMode = ServerSentEventsKeepaliveMode.Always;
options.KeepaliveInterval = 15;
options.KeepaliveCommentValue = "SOMETHING";
});
...
}
...
}
Which would result in pushing following comment to the stream.
: SOMETHING
This wouldn't be complex and can be considered a non-breaking change with proper default, but I still would like to understand the value behind adding the ability to set a value of something which should be ignored on the client side.
Hey @tpeczek i'm using this lib and could not receive any heartbeat events with it. They say in the docs that this is needed to send every 15-30 seconds and it has to be a comment but for some reason it does not work with the comment that is sent here. I since switched to another better lib which does not require any heartbeat events to be sent: https://www.npmjs.com/package/eventsource
Maybe the format of the comment is expected differently? Or maybe its just a bug in the frontend lib.
Hi @cwirz,
Both options are possible. It can be a bug in client implementation when it comes to handling comments, but it can also be a bug in my implementation. For example I'm not sending comment as an isolated block (followed by an additional blank line). This is perfectly valid from protocol specification point of view, but if a client library has alive checks after the parser, the parser might yield no activity as no full block has been received.
This is why I want to understand this deeper, because maybe changing keep alive to always send comments in isolated blocks is the actual needed thing.