Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The keywords in this problem is "inter-tab communication", "cross-window messaging", etc...</p> <p>One solution is similar to long-polling: <a href="http://blog.fastmail.fm/2012/11/26/inter-tab-communication-using-local-storage/" rel="nofollow noreferrer">inter-tab-communication-using-local-storage/</a> Periodically ask the localStore/cookies for changes, and add a queue to allocate common resources (e.g. socket.io connection). You can use onbeforeunload or timeout to determine whether a tab/window is navigated away or closed. After that shortly the next tab in the queue will allocate the resource...</p> <p>The second solution is to use "localStore storage events" for the same. In that case you don't have to periodically ask the localStore (if onbeforeunload event is available). According to this: <a href="https://stackoverflow.com/questions/7428671/local-storage-events">localStorage eventHandler Doesn&#39;t Get Called</a> storage events are designed to affect only other tabs, so they are a good choice for intertab communication. The only problem is onunload: <a href="https://stackoverflow.com/questions/13671613/local-storage-on-window-unload-event">local storage on window unload event</a> . So because of the lack of onunload support the first solution could be better.</p> <p>The third solution would be the usage of "shared webworkers", but they have not been implemented yet in several browsers (internet explorer), or they cannot open socket (firefox). So currently they are not an option, maybe 1-2 years later after bug fixes... Here is a demo - works chrome only - <a href="http://gh-pages.coolaj86.com/html5-shared-web-worker-examples/" rel="nofollow noreferrer">html5-shared-web-worker-examples</a>.</p> <p>The fourth solution would be <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage?redirectlocale=en-US&amp;redirectslug=DOM/window.postMessage" rel="nofollow noreferrer">window.postMessage</a>, which has not complete browser support currently. I read about it in some sto questions, and they all wrote that postMessage is not capable for what we want. I did not check the exact details about that function, it does not worth the time I think... There is an example about cross domain cross iframe communication: <a href="https://stackoverflow.com/questions/1949764/cross-domain-iframe-communication">Cross-Domain iframe communication</a> but same domain cross window communication is not possible with that I think.</p> <p>The fifth solution would be using cookies but in that case every tab should ping document.cookie because there in no cookie change event, like storage event in localstore. <a href="http://theprivateland.com/bncconnector/home.htm" rel="nofollow noreferrer">BNC Connector</a> uses this approach.</p> <p>The sixth solution would be using WebSQL. Its driver is async non blocking, so it would be better than localStorage, but currently it is not supported by firefox and msie.</p> <p><strong>Conclusion:</strong></p> <p>Nowadays - 2013.10.03 - the best option to periodically ping the localStore from the resource utilizer tabs. The other tabs should listen to the storage event of the timestamp updates. If that event does not come in time, than the resource utilizer tab has timeout, and the next tab in the queue should get the resource. Maybe later the onunload event or the shared workers will be reliable, but currently they are not good enough...</p> <p><strong>Solution:</strong></p> <p>I found an implementation of the approach described in the conclusion: <a href="https://github.com/diy/intercom.js" rel="nofollow noreferrer">intercom.js</a> I added an <a href="https://github.com/diy/intercom.js/issues/3" rel="nofollow noreferrer">issue</a> about a general interface of resource sharing, but in my case the single socket.io resource is good enough...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload