Server Side Events - Expected Lag

First, this was extremely easy to setup for anyone else looking at using SSE. My question is generally speaking what is the expected lag time for sending an event (ignoring network latency etc). I’m doing some local testing of sending events when an api receives a post and then another when the data has been retrieved and prior to being sent back. Just a simple demo. I’m noticing:

User presses button to cause ajax post

  • Event is sent
  • Event received almost immediately
    Method retrieves the data from the backend
  • Event is sent
    Browser gets Response
    Browser updates page (using vue)
  • Event received 1-2 seconds after it was sent.’

In the documentation it states to disable dynamic compression on iis to avoid buffering, but not sure if that applies to Core (1.2) or not? Is this normal or should it be nearer to real time?

SSE events are normally instant, you can see this in the Chat demos for .NET 4.5 and .NET Core which each chat message sent calls an API on the server that sends a SSE message to all channel subscribers who display it in their chat log:

I’m wondering if posting it via an endpoint is causing the lag, I’m basically proxying all of them via a service:

ConsoleService.Any(new ConsoleEvent() { ConsoleEventType = ConsoleEventType.ContactReturned, SessionId = this.Request.GetSessionId() });

I’m going to refactor that to not use the service and see if it is any better.

I refactored it to remove the intermediate service and it is still slow especially compared to the demos. The code is pretty basic and the only other change from mine (running in visual studio) and the demo that I saw as I’m using notifysession. I’m wondering if notifysession is slower?

ServerEvents.NotifySession(this.Request.GetSessionId(), new ConsoleEvent() { ConsoleEventType = ConsoleEventType.ContactSubmitted, SessionId = this.Request.GetSessionId() });
var cached = TableService.Get(pe);
if (cached!=null)
{
ServerEvents.NotifySession(this.Request.GetSessionId(), new ConsoleEvent() { ConsoleEventType = ConsoleEventType.ContactReturned, SessionId = this.Request.GetSessionId() });
return cached;
}

Are you sure it’s slower? How much faster is calling the other ServerEvents Notify APIs for you?

I don’t see why it would be noticeably slower, all GetSessionId() does is fetch the Session Id from the current IRequest.

I am seeing delays up to 5 seconds. I switched to notifychannel it made no difference. I checked to see if it was vue but it console logs the sse event the same time vue updates. I launched a second browser and it delays a second from updating them both. 3 seconds browsing 1, and 4 seconds browser 2.

The chat demo apps are fast but I’m not seeing that speed in my local (or published) environment.

This is the full code that is running (it is always returning from the “cache”):

 ContactResponse cr = new ContactResponse();
        
            ServerEvents.NotifyChannel(this.Request.GetPermanentSessionId(), new ConsoleEvent() { ConsoleEventType = ConsoleEventType.ContactSubmitted, SessionId = this.Request.GetSessionId() }); // fast yes
            var pe = new PersonEntity(request);
            pe.Ip = Request.RemoteIp;
            var cached = TableService.Get(pe);
            if (cached!=null)
            {
                ServerEvents.NotifyChannel(this.Request.GetPermanentSessionId(), new ConsoleEvent() { ConsoleEventType = ConsoleEventType.ContactReturned, SessionId = this.Request.GetSessionId() }); // takes seconds to receive
                

                return cached;
            }

I am going to run the chat demo app and see if it performs the same.

I added the web.config to the core app, didn’t think it was going to be necessary. Works as expected in the browsers that support it (ie/edge suck). Is there a way to disable url compression without it?

Is there a way to disable url compression without it?

There isn’t AFAIK

Okay… Now to find a polyfill that works… Thanks for your help as usual.

1 Like