Wormhole
Is EventEmitter for communication between tabs.
npm i --save-dev wormhole.js
Features
- One connection on WebSocket for all tabs.
- Support Master/Slave
- Cross-domain communication
- SharedWorker or fallback to localStorage
- IE 8+, Chrome 10+, FireFox 10+, Opera 10+, Safari 6+
- Test coverage (run)
Basic example
// All tabs; // Some tab; // Master tabif master // .. ;
One connection on WebSocket for all tabs
Module wormhole.js/ws
implements WebSocket-like interface:
https://rubaxa.github.io/wormhole/?ws=y
; // Create WebScoket (wormhole-socket)const socket = 'ws://echo.websocket.org'; // OR new WS('...', null, hole); socket console;socket console; // Unique eventsocket { console;}; // Some tabsocket // All tabs:// "Received:" {foo: 'bar'}
CORS example
- Create a subdomain, ex.:
http://wormhole.youdomain.com/
; - Copy-paste universal.html into root;
- Check access
http://wormhole.youdomain.com/universal.html
; - Profit.
// http://foo.youdomain.com/var hole = 'http://wormhole.youdomain.com/universal.html'; hole; // http://bar.youdomain.com/var hole = 'http://wormhole.youdomain.com/universal.html'; hole;
Master/slave example
// All tabs { var _cache = {} { return requrl + JSON; } ; // Define remote command (master) 'get-data' = { var key = promise = _cachekey; if !promise _cachekey = promise = $; return promise ; }; // Get remote data $ { var dfd = $; // Calling command on master (from slave... or the master, is not important) ; return dfd; }; // I'm master! ;}jQuery; // Tab #X$; // Tab #Y$;
Peers
;
Executing the command on master
// Register command (all tabs)'foo' = { // bla-bla-bla ; // or `next('error')`}; // Calling the command (some tab)
Modules
wormhole.Emitter
Micro event emitter.
- on(type:
String
, fn:Function
):this
- off(type:
String
, fn:Function
):this
- emit(type:
String
[, args:*|Array
]):this
var obj = wormholeEmitter; // or new wormhole.Emitter(); obj; obj; // []obj; // [1]obj; // [1, 2, 3]
wormhole.cors
Wrapper for postMessage
.
// Main-framewormholecors; wormholecors'some:command' = { return value * 2;}; // IFramewormhole;wormhole;
wormhole.store
Interface for localStorage
.
- get(key:
String
):*
- set(key:
String
, value:*
) - remove(key:
String
):*
- on(type:
String
, fn:Function
) - off(type:
String
, fn:Function
)
wormholestore; wormholestore; wormholestore;// change -> foo {bar: 'baz'} wormholestore;// change -> prop {qux: 'ok'}// change:prop -> prop {qux: 'ok'}
Utils
String
wormhole.uuid():A universally unique identifier (UUID) is an identifier standard used in software construction, standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE) (c) wiki.
Function
, delay:Number
[, immediate:Boolean
]):Function
wormhole.debounce(fn:Development
npm test
npm run dev
— run dev watchernpm run build