throttling SignalR to use long polling with short-lived connections

throttling SignalR to use long polling with short-lived connections

if you are using SignalR for notifications rather than for real-time up to date data,
then it may be useful to limit how many connections and for how long they are open,
to avoid hogging server sockets.

Adjusting setting on server: [C#]

 /* SignalR config:  
         This setting represents the amount of time to leave a transport connection open and waiting for a response before closing it and opening a new connection. The default value is 110 seconds.  
         This setting applies only when keepalive functionality is disabled, which normally applies only to the long polling transport. The following diagram illustrates the effect of this setting on a long polling transport connection.  
        * */  
 GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(2);  

Setting client to be long-polling: [JavaScript]

 //longPolling: do NOT keep connections open, as it exhausts the server limit:  
 $.connection.hub.start({ withCredentials: true, transport: 'longPolling' })  

Then the socket is no longer always kept open by SignalR.

This can be important if you want users to be able to open multiple tabs to your site,
since browsers limit the number of open sockets, per-host:
http://sgdev-blog.blogspot.nl/2014/01/maximum-concurrent-connection-to-same.html

Reference:
http://www.asp.net/signalr/overview/guide-to-the-api/handling-connection-lifetime-events#changetimeout

Comments