简体   繁体   中英

How can I improve ServiceStack Server Events Efficiency

I was looking at replacing our periodic polling web page with ServiceStack server events, but in looking at the behavior, the server events mechanism is actually way more overhead than what we were doing - return message in our case is a couple hundred bytes and only polls every five seconds - vs heartbeat of 496bytes every few seconds and the eventstream long poll looks like even more than that on average. Combined maybe 2-3Kbps per client.

I don't care too much about latency but I do care about the number of connections and bytes transferred. Is there any configuration that makes the javascript based client a little more efficient?

    var source = new EventSource(
       '/event-stream?channel=someUpdate&t=' + new Date().getTime());
   $(source).handleServerEvents({
       receivers: {

           some: {
               Update: function(id) {
                   alert(id);
               }

           }
       },
   });

The SSE Server Events connection uses only a single long-lived HTTP Connection per subscription and the events themselves are a lot more efficient than polling as it just sends the event data instead of the overhead of a new HTTP Request and repeated HTTP Headers each time.

It sounds like you're measuring the heartbeats used to detect whether the SSE connection is still alive which uses the following defaults :

IdleTimeout = TimeSpan.FromSeconds(30);
HeartbeatInterval = TimeSpan.FromSeconds(10);

Note the number of heartbeats remain constant per client, ie they don't change whether connected clients receive several events per second - clients still only send 1 heartbeat every 10 secs.

We've got customers handling several thousand concurrent connections with these defaults but you can change the heartbeat intervals when you register the ServerEventsFeature , eg you can double the intervals with:

Plugins.Add(new ServerEventsFeature {
   IdleTimeout = TimeSpan.FromSeconds(60),
   HeartbeatInterval = TimeSpan.FromSeconds(20),
});

To reduce the bandwidth a bit from the heart beat, you could override the ServerEventsFeature OnCreated method to strip the default profileUrl as this is probably a decent chunk of the heartbeat event in terms of bandwidth per heart beat, assuming you aren't using it . For eg,

Plugins.Add(new ServerEventsFeature
{
    StreamPath = "/event-stream",
    HeartbeatPath = "/event-heartbeat",
    UnRegisterPath = "/event-unregister",
    SubscribersPath = null,
    LimitToAuthenticatedUsers = false,
    NotifyChannelOfSubscriptions = false,
    OnCreated = (subscription, request) =>
    {
        subscription.Meta.Remove(AuthMetadataProvider.ProfileUrlKey);
    }
});

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