post-message-event-emitter
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-alpha.2 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i post-message-event-emitter

Weekly Downloads

0

Version

1.0.0-alpha.2

License

MIT

Unpacked Size

3.61 kB

Total Files

4

Last publish

Collaborators

  • linusu