TrainmanJS
TrainmanJS is Cross-Origin Communication Library. It's a
wrapper for the native postMessage
API.
It allows you to send bidirectional Topics and Messages between the iframes and wrapper (parent) window.
The Trainman is an exile who created and operates Mobil Avenue and is a servant of another exile program known as the Merovingian.
source: http://matrix.wikia.com/wiki/The_Trainman
Features
-
Topics and messages
You can listen to different kind of topics and message to it. You have control to which frame you would like to send a message. What is also important, the parent frame knows exactly which child frame did send the message. Thanks to that you can omit some topics from particular recipients inside your topic listener.
-
Lazy loading
Thanks to the queuing mechanism and buffer you can start sending messages immediately. You can send a message from the wrapper (parent) window to the child iframe even it's still loading. Queued messages will be delivered once the connection is established.
-
Reconnection
When the child frame will reload itself, Trainman will try to reconnect with it. During the reconnection, Trainman will queue messages inside his buffer. Queued messages will be delivered once the connection is established again.
-
Bulk operation (upcoming)
Post a message to all child frames at once. Don't need to specify an exact recipient. You can use the '*' symbol for sending a message to all of the connect children frames.
-
Debug mode
Easily turn on and off logging mechanism. Thanks to that you can debug your code and application in a much more simple way. You can track and check whole Trainman and Passenger communication.
-
F2F (upcoming)
Frame-to-frame direct communication. Allow to send and proxy messages directly between the child frames.
How to install
NPM and Yarn
You can install the library using NPM:
npm install trainmanjs -S// oryarn add trainmanjs
Next, you can import TrainmanJS module into your app:
;
For the child pages (frames) you should import Passenger class:
;
Using CDN
You can also use the CDN service ex. unpkg.com or npm-cdn.herrokuapp.com
<!-- or -->
Then you can access the Trainman and Passenger from the injected module:
{ const Trainman = moduleTrainman; const Passenger = modulePassenger;}windowtrainmanjs;
How to use it
TBA
Examples
Simple Example
- Check simple example how to use Trainman
- Here you can find the source code for the above example
Re-connection and Debug Mode
- Reconnection example
- Here you can find the source code for the above example