postaljs / postal.xframe

postal.js add-on providing the ability to federate instances of postal across iframe/window boundaries
Other
36 stars 38 forks source link

communicate between windows? #8

Open mvolkmann opened 11 years ago

mvolkmann commented 11 years ago

Is it possible to use postal.xframe to communicate between two browser windows where neither is an iframe? I thought perhaps if the first window used window.open() to open the second, it would work. However, I haven't been successful with that so far.

ifandelse commented 11 years ago

@mvolkmann - I've been meaning to experiment with this myself to see. In theory it should work, or at least be simple to support assuming the browser actually supports postMessage between parent and "child-via-window.open()" window. I've got a few tasks in front of this, but if I can play around with it, I'll keep you posted. In the meantime, what browser(s) have you tried it in?

mvolkmann commented 11 years ago

Thanks Jim! I tested this in Chrome 26.0.1410.65.

For now I'm able to get HTML5 "Cross-document messaging" and "Channel messaging" to work.

BTW, if you Google "James Cowart" and click "Images" you'll see a large number of scary looking police mugshots. I'm sure none of them are you. ;-)

Nemo157 commented 10 years ago

This works fine (in Chrome), at least when explicitly signalling ready to the window opener. I've got a little test case that has a main page with simply <a target="sub" href="sub.html">Open Subpage</a then in the subpage:

postal.instanceId('sub');
postal.fedx.configure({
  filterMode: 'blacklist'
});
postal.fedx.signalReady({
 xframe: { target: window.opener }
}, function () {
  postal.publish({
    channel: 'c',
    topic: 't',
    data: 'data'
  });
});

which gets the message back to the opening window fine.

Nemo157 commented 10 years ago

It's failing in IE11 though, first because IE11 still doesn't allow postMessage to be applyd. Hacking the check for noPostMessageApply for this it then successfully sends the message, but it never appears in the receiving window.

Demo: http://jsfiddle.net/MLr5x/show/

Just tried changing to using window.open rather than a link and it does work in IE11: http://jsfiddle.net/MLr5x/2/show/

ifandelse commented 10 years ago

Hi @Nemo157, thanks for providing the fiddles! I'll try and check those out here this week. I'm currently trying to wrap up some changes to machina.js, but will look into this as soon as I get to a stopping point. Thanks!

mteichtahl commented 9 years ago

has there been any progress with this ?

ifandelse commented 9 years ago

@mteichtahl Apologies - I haven't looked too deeply into this as most of my time has been focused on other projects at the moment. I did spike out an experimental idea of using local storage to enable communication between tabs, and I believe @dcneiner has a fork somewhere that supports window-to-child-window using xframe. I'll check with him and see if that's the case.

mteichtahl commented 9 years ago

thanks jim

Rather than local storage, should we look at shared web workers ? or the like ?


Marc Teichtahl marc@teichtahl.com Ph: +61 434 100 259 http://www.teichtahl.com

On 1 Feb 2015, at 5:33 pm, Jim Cowart notifications@github.com wrote:

@mteichtahl https://github.com/mteichtahl Apologies - I haven't looked too deeply into this as most of my time has been focused on other projects at the moment. I did spike out an experimental idea of using local storage to enable communication between tabs, and I believe @dcneiner https://github.com/dcneiner has a fork somewhere that supports window-to-child-window using xframe. I'll check with him and see if that's the case.

— Reply to this email directly or view it on GitHub https://github.com/postaljs/postal.xframe/issues/8#issuecomment-72353837.

ifandelse commented 9 years ago

@mteichtahl Oh, definitely agree that shared workers are the longer-term ideal. I'm concerned about widespread support, though. Safari briefly supported them (desktop and mobile, if I recall correctly) and then ripped support out. IE has no plans at the moment to support them. Since it's a feature, then, that would only be FF/Chrome, it's not been a priority overall.

moldoe commented 7 years ago

Has there been any progress with this? We already use postaljs for messaging in our single page javascript application but we need to allow multiple instances of the applications to run in multiple browser windows/tabs. Having support for this in postaljs would be ideal, otherwise we would need to glue the communication using other libraries or custom code. Thanks!

mteichtahl commented 7 years ago

+1

moldoe commented 7 years ago

Starting from the fiddles provided by @Nemo157 I was able to create a couple of JSFiddle pages that prove that this type of messaging works between browser windows. The only issue was that while postal.js was updated to work with lodash 4, postal.federation and postal.fedex were not. They only work with lodash 3. That is why the JSFiddle external resource points to lodash 3.10.1.

The first fiddle contains the code for Index page. Runtime Page: https://jsfiddle.net/zwb58egf/12/show

Fiddle Code: https://jsfiddle.net/zwb58egf/12/

The second fiddle contains the code for the Sub page, which when loaded will publish a message which will show up in the Index page.

Fiddle Code: https://jsfiddle.net/Lbb3tc41/1/

This was tested on Chrome, Firefox, IE11 and Edge.

Thank you for this great library!

geeklisted commented 7 years ago

@moldoe and others: I'd very much like to implement the same cross-window (not just iFrame) browser messaging but I found that the example you provided may no longer work. In my own experiments, I have yet to accomplish the goal of having two browser windows (one which is a child to a 'parent') communicate with each other via postal.xframe but it clearly seems that it should be possible.

Part of my confusion maybe related to the documentation surrounding postal.fedx.signalReady() and the arguments it takes. I've seen it two different ways now: postal.fedx.signalReady("xframe"); postal.fedx.signal( { xframe: { target: window.opener } } ); ^^ But I've also read that one must provided a second argument due to logic in postal.federation.

Has anyone explored this concept more than what has been mentioned above?

Thanks.

ifandelse commented 7 years ago

@moldoe thanks so much for the fiddles! About to check them out now. Also - sorry for the lodash version woes. I'm hoping to update the postal libs across the board to lodash 4 here in the near future (among other cleanup and improvements).

@geeklisted the API could definitely stand to improve, sorry for the confusion. The postal.federation wiki has some info on signalReady that might be helpful. It's amazing how much my own opinions have changed since I wrote that API years ago. While I've been using these projects in production for a while, I'm trying to revisit them with a fresh perspective and hopefully apply some of the lessons learned over time. I want to keep the kinds of flexibility that exists in lower-level configuration where possible, but I want to optimize more in the direction of "plug it in and things connect without intervention".

geeklisted commented 7 years ago

@ifandelse We are using postal.xframe correctly now and are happy with the way its working. Any chance we could get wildcards on channel names? :)

crxgames commented 7 years ago

@geeklisted What did you end up having to do to get it working? I'm currently stuck in the same spot you are.