reTHINK-project / core-framework

The main goal of WP3 is to provide the reTHINK core framework comprised by the runtime environment where Hyperties are executed and the messaging nodes used to support messages exchange between Hyperties.
Apache License 2.0
1 stars 0 forks source link

How to use a reTHINK runtime deployed in a different tab #137

Closed antonroman closed 7 years ago

antonroman commented 8 years ago

I was wrong regarding the ability to send postMessages to different tabs from different domains setting a target URL as a second parameter in the postMessage(), so @shumy 's approach (briefly discussed during Heidelberg integration session) seems to be the only valid to me. I guess we could follow the approach defined in this stackoverflow questions.

It's not the best solution because (we need to load both iFrames from the same domain), but it's the only solution I can figure out within a browser:

Copied from the links:

It can be done if you use an "intermediate page" loaded in an iFrame.

The (theoretical) solution uses two separate methods of inter-page communication:

  1. window.postMessage()
  2. localStorage or indexDB (for this localstorage makes more sense to me)

The "intermediate page" acts as a proxy, translating message events into localStorage events, and vice-versa. If you load this "intermediate page" in an iFrame from both pages, then any messages you post in one tab will pop out in the other tab:

[Tab 1] --(postMessage)--> [iFrame 1]
                                 |
                                 v
                          (localStorage)
                                 |
                                 v
                           [iFrame 2] --(postMessage)--> [Tab 2]

If one of the tabs is on the same domain as the intermediate page (illustrated here as Tab 2), this can be simplified (without affecting the other tab's setup).

[Tab 1] --(postMessage)--> [iFrame 1]
                              |
                          (localStorage)     
                              |
                              v
                          [Tab 2]

In order to get notified when a tab sends a message to other tabs, you simply need to bind on 'storage' event. In all tabs, do this:

$(window).on('storage', message_receive);

The iFrame could be the reTHINK iFrame we already have for browser runtime. The inital task to be done by the iFrame code would be to discover if there is another rethink runtime, by either just checking the existence of a variable in the localstorage (not sure if valid as localstorage is persistent) or by trying to send a message to other iFrame:

Looking forward to reading your thoughts on this.

pchainho commented 8 years ago

Another reference for the localStorage solution:

https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker

Another option is the shareWorker which seems more appropriate:

https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker

pchainho commented 8 years ago

The Channel Messaging API seems promising.

In phase 2 we should also look on whether it may contribute on having the Messaging Framework supported more natively by Browsers.

pchainho commented 8 years ago

If SharedWorker can be accessed from several browsing contexts, all those browsing contexts must share the exact same origin (same protocol, host and port).

I guess this means Applications have to be loaded from the same domain in order to share the same runtime instance that would be executed as a SharedWorker ...

It seems to be possible to have dedicate workers created and managed by a SharedWorker ie to have the protostub sandbox implemented as a dedicated worker inside a ShareWorker.