Skip to content

A small wrapper around the postMessage interface to simplify event handling.

Notifications You must be signed in to change notification settings

honeinc/browser-event-bus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

browser-event-bus

A simple wrapper around the postMessage API to simplify event handling.

Usage

// top level document

var EventBus = require( 'browser-event-bus' );

// listen for all events in the 'foo' namespace
var eventBus = new EventBus( {
    namespace: 'foo', // default: ''
    target: '//foo.com', // default: '*',
    origin: '//foo.com', // default: '*'
} );

eventBus.on( 'pong', function( msg, thing ) {
    console.log( msg );
    console.log( thing );
} );

// we also pass the actual postMessage event as the last argument to the
// handler. it can be useful for getting the origin element, etc.
eventBus.on( 'yak', function( yak, event ) {
    console.log( event );
} );

eventBust.emit( 'ping' );
// in iframe or other tab

var EventBus = require( 'browser-event-bus' );

var eventBus = new EventBus( {
    namespace: 'foo'
} );

eventBus.on( 'ping', function( msg ) {
    eventBus.emit( 'pong', 'hi!', {
        thing: true
    } );
} );

About

A small wrapper around the postMessage interface to simplify event handling.

Resources

Stars

Watchers

Forks

Packages

No packages published