tab-emitter

1.0.11 • Public • Published

tab-emitter

An event emitter for same-origin tab communication

Build Status

tab-emitter is a client-side javascript module that allows you to send events between browser tabs/windows. The sending/recieving web pages must have the same origin. You can not emit events between browsers, only between the same browser.

tab-emitter is written to work with browserify, and is extremely easy to implement in your code.

Demo

demo site

Example

client1.js

var TabEmitter = require('tab-emitter')
var emitter = TabEmitter()
 
setTimeout(function () {
    emitter.emit('hello', { thing: 'world' })
}, 5000)
 
emitter.on('hello', function (obj) {
    console.log(obj.thing) // => 'world'
})

client2.js

var TabEmitter = require('tab-emitter')
var emitter = TabEmitter()
 
emitter.on('hello', function (obj) {
    console.log(obj.thing) // => 'world'
})

Don't use browserify?

If you just want to use this module in the browser without dealing with browserify, here's how you can:

<script src="https://wzrd.in/standalone/tab-emitter@1"></script>
<script>
    var emitter = window.tabEmitter()
    
    emitter.on('event', function () {
        console.log('event just happened')
    })
    
    setTimeout(function () {
        emitter.emit('event')
    }, 5000)
</script> 

API

var TabEmitter = require('tab-emitter')

var emitter = TabEmitter([key])

  • key is a key to uniquely identify an emitter across tabs. If the same key is used in multiple tabs, they can communicate with each other.
  • Returns emitter which is an EventEmitter instance.

Install

With npm do:

npm install tab-emitter

License

MIT

Package Sidebar

Install

npm i tab-emitter

Weekly Downloads

362

Version

1.0.11

License

none

Last publish

Collaborators

  • artskydj