Skip to content

LinusU/post-message-event-emitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Post Message Event Emitter

An EventEmitter interface on top of postMessage.

Installation

npm install --save post-message-event-emitter

Usage

parent:

const PostMessageEventEmitter = require('post-message-event-emitter')

const other = window.open('http://localhost:3001/other-page')
const events = new PostMessageEventEmitter(other, 'http://localhost:3001')

events.on('hello', (name) => {
  console.log(`Hello, ${name}!`)

  events.emit('ping')
})

child:

const PostMessageEventEmitter = require('post-message-event-emitter')

const events = new PostMessageEventEmitter(window.opener, 'http://localhost:3000')

events.emit('hello', 'World')

events.on('ping', () => {
  console.log('Received ping')
})

API

new PostMessageEventEmitter(other, origin)

Create a new PostMessageEventEmitter instance. The other parameter is usually window.opener or the return value from window.open. The origin should be set to the origin with which you want to communicate with.

.on(name, fn)

Register a handler for an event with the name name. The provided fn will be called every time the other side calls emit.

.emit(name, data)

Emit an event to the other side. Their handler will be invoked with the data provided.

.stop()

Stop listening for incoming events.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published