Inter-Process-Communication (IPC) Library for JavaScript. This Library enables communicating between two windows on different domains. The communication can be bidirectional, so every window itself can be both a sender and a recipient. To make the communication between two windows work, both of them must include the same version of msg-js-ipc
.
msg-js-ipc
is based on please.js, a Request/Response based wrapper around the PostMessage API that makes use of jQuery Promises. please.js itself is based on top of jQuery and the jQuery Promise API.
Before start using msg-js-ipc
, you must make sure that both windows have msg-js-ipc
loaded and thus the global object IPC
is available.
Install with npm
$ npm install msg-js-ipc
or download source.
##Usage
###Sender
To send an event, just call the method sendEvent
on the global object IPC
.
IPC.sendEvent(window.parent, 'ipc_infoText', text)
The method sendEvent
has the following parameter:
- The target window (recipient)
- The name of the recipient's method as string
- [optional] further parameters (0-n), as many as the implementation of the given method expects
For example we have a window A that includes an iFrame B. Then the target window of A is the iFrame B ($('iframe').get(0).contentWindow
) and the target window of the iFrame B is the window A (window.parent
).
In case the sender tries to call a method the recipient has not implemented, an error is thrown from the
msg-js-ipc
Library.
###Recipient
To receive an event, the recipient must register its implementation on the global object IPC
.
IPC.registerRecipient(this)
Furthermore the recipient has to implement the methods, that the sender wants to call.
ipc_infoText (text) {
// the implementation of the method
// here u can give the information back in the application and convert it to the applications architecture
},
All methods, the recipient provides for the communication with a sender, must start with the prefix
ipc_
. That is for security reason to avoid that everything the recipient implements can be called from a sender. Only through the prefixipc_
the method is available for a sender. If the sender tries to call a method, that does not start with the prefixipc_
, an error is thrown from themsg-js-ipc
Library.