tejacques / callr

A SignalR Utility Library (js and csharp)
MIT License
7 stars 0 forks source link

Share connection across multiple tabs #9

Open leewang0 opened 10 years ago

leewang0 commented 10 years ago

It might be possible to share one signalR connection across several browser tabs by abusing local storage. CallR could, (though probably shouldn't by default), store state in browser's local storage on whether or not it has an open SignalR connection to a particular hub. If it does, then when another browser tab tries to connect to it, intercept the connect call and instead pass messages to the tab with the connection via local storage. This assumes that the other browser tabs will periodically check local storage to see if the one connection has returned the results of their call, effectively long-polling it. Since there's no network latency involved in checking local storage, though, this should be an ok way to do it as long as a clever way to store signalR requests and tab-origins can be put in local storage.

tejacques commented 10 years ago

I think this is a neat idea, but there are a ton of edge cases / tricky things to work out, here are four off the top of my head.

I think these could all be addressed but it adds a huge amount of complexity. The benefit of adding this feature is that no more than one connection per browser would be made to signalR, which is great, but probably not the most common use case.

I'll consider this as a future feature but there are other easier ways of getting past the n-connection limit, like having multiple subdomains, etc.

tejacques commented 10 years ago

After doing some more research, it does look like atomic mutexes can be utilized to get past localstorage concurrency, and StackOverflow actually uses this trick in their chat:

I think it could work like this:

Other resources: