SignalR on ios and a single domain

Safari on ios has a limitation that you can only have one concurrent request to a particular domain at a time. Normally this is fine, since once a request completes the next one that is queued up fires off. But what if you are using a realtime persistent connection library like signalR? In this case your one allowed connection is held up with the signalR request. If you’re not on a mac or linux and you use windows 7 or earlier you can’t use websockets so you’re stuck using http. Most suggestions involve buying a second domain, but sometimes thats not possible, especially if your application is a distributable web app that can run on client machines. You can’t expect clients to have to buy a second domain just so your realtime push works.

A nice solution, posted about in this github tracker following the issue is to configure your signalR’s long poll mechanism to work in short bursts. Instead of having a single persistent connection, you can tell the client to use the long poll transport (using typescript here)

// we can’t do signalR push with an iphone since
// iphones only allow for single connections, so having the
// forever frame interferes with other resource loading
$.connection.hub.start(signalRConfig, () => console.log("connected"));
[/ts]

But when I did this, per the tracker suggestion, I saw that I was still getting bursts that lasted 5 seconds. This was too long for me, since for 5 seconds you can’t do anything else. If you are doing a lot of dynamic calls (making AJAX requests, or other service calls) then 5 seconds really holds up the application.

When in doubt, read the source. I’m unfortunately using a really old version of signalR because I’m tied to that version due to how my application is distributed (and that signalR versions aren’t backwards compatible), so this advice may not apply to later versions. But, I found what the issue was.

SignalR uses a heartbeat to check when things are disconnected or timed out:

So, that means that no matter what you set the disconnect and connection timeouts to be, they can’t be any more granular than 10 seconds. If you remember from signal processing the shannon-nyquist sampling theorum

You know that your sampling frequency needs to be twice the target frequency. Fancy words for sample more to get more granular