Git Product home page Git Product logo

postmessagechannel's Introduction

Build Status

postMessageChannel

Cross-domain channel communication. Provides similar functionality to pmrpc and jschannel but with an alternative API.

postMessageChannel allows you to setup a channel between two frames. You may then send data to functions of the other frame and receive data back. An ID is defined for a channel so that multiple channels may exist on a page and not conflict.

Examples

parent.html

<!doctype html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>This is the parent</title>
    <script src="postMessageChannel.js" type="text/javascript"></script>
  </head>
  <body>
    <iframe id="childFrame" src="child.html"></iframe>
    <script type="text/javascript">
      (function () {
        var pmc = postMessageChannel({
          target: document.getElementById('childFrame').contentWindow,
          origin: window.location.origin,
          id: 'myScope'
        });

        var result = pmc.run('hello', { subject: 'world' });

        // run() returns a promise we can use to receive data back from the other frame
        result.then(function (data) {
          console.log('Got back', data);
        });
      }());
    </script>
  </body>
</html>

child.html

<!doctype html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>This is the child</title>
    <script src="postMessageChannel.js" type="text/javascript"></script>
  </head>
  <body>
    This is the child iframe.
    <script type="text/javascript">
      (function () {
        var pmc = postMessageChannel({
          target: parent,
          origin: window.location.origin,
          id: 'myScope',
          methods: {
            hello: function (data) {
              data = data || {};
              return 'hello, ' + data.subject + '!';
            }
          }
        });

      }());
    </script>
  </body>
</html>

Timeouts

If you want requests to timeout, pass a third parameter to run():

pmc.run('dummy', null, 1000).then(
  function success () {
    console.log('success'); // This won't run.
  },
  function failure () {
    console.log('This timed out! (Expected)');
  }
);

Async Methods

Methods may be asynchronous. Use this.async(), then resolve() or reject() the returned deferred object.

var pmc = postMessageChannel({
  target: parent,
  origin: window.location.origin,
  id: 'myScope',
  methods: {
    asyncFn: function (data) {
      var dfd = this.async();
      window.setTimeout(function () {
        dfd.resolve('result');
      }, 1000);
    }
  }
});

postmessagechannel's People

Contributors

jasontbradshaw avatar owiber avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

spredfast

postmessagechannel's Issues

Send error from child frame and sychronous methods

JSChannel allows to send error messages using the following syntax:
throw {error: "object_error_code", message: 'object error message'};

The only way I found to send error from child to parent in your lib is through the use of the async method reject.
However, is there a way to achieve this when using synchronous methods ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.