Giter Club home page Giter Club logo

tab-emitter's Introduction

tab-emitter

Emit events between browser tabs! (Same-origin only.)

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. It should work with Rollup and Webpack too.

demo site

Example

client1.js

var TabEmitter = require('tab-emitter')
var emitter = TabEmitter()

setTimeout(() => {
	let data = { x: 'world' }
	emitter.emit('hello', data)
}, 5000)

emitter.on('hello', data => {
	console.log(data.x) // 'world'
})

client2.js

var TabEmitter = require('tab-emitter')
var emitter = TabEmitter()

emitter.on('hello', data => {
	console.log(data.x) // '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://bundle.run/[email protected]"></script>
<script>
    var emitter = window.tabEmitter()
    
    emitter.on('event', () => {
        console.log('event just happened')
    })
    
    setTimeout(() => {
        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.

emitter.emit(eventName, [...args])

Emits an event to its own browser tab, as well as to other browser tabs of the same-origin.

  • eventName is a string. emitter.on will watch for this string.
  • You can have any number of args. They must be JSON serializable.

emitter.on(eventName, handler)

Watches for events on other browser tabs of the same-origin, as well as its own browser tab.

  • eventName is a string of the event name to watch for.
  • handler(...args) is a function that will be called every time the event with the given name is emitted.
    • ...args are the arguments passed to emitter.emit(eventName, ...args). The eventName is not included.

emitter.once, emitter.removeAllListeners, emitter.off, etc.

For more extended documentation, please look at the official EventEmitter documentation. Technically, EventEmitter is getting polyfilled by your choice of bundler. Likely with this module. But the API should be identical.

Install

With npm do:

npm install tab-emitter

License

MIT

tab-emitter's People

Contributors

artskydj avatar tehshrike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

tab-emitter's Issues

travis

get the build working on travis

Make a sweet logo

I'm thinking of a screenshot of 2 chrome tabs, with arrows between.

Like this but prettier and more obvious what is what

image

Don't use tape-results for the tests

Don't use tape-results for the tests?

  1. There has to be ONE server for both test files. Otherwise the urls have different origins, and localstorage isn't shared.
  2. Both files need to be opened in the same browser. Perhaps open or opener is the best choice?
  3. The terminal needs a way to know if the tests passed or failed.
    • There could be a server (I think it could have a different origin) that listens for localhost:8080/pass and localhost:8080/fail. Then the tests would have to ping that endpoint.
{
    "scripts": {
        "build1": "browserify test/relay.js -do test/serve/relay.js && browserify test/assert.js -do test/serve/assert.js",
        "serve": "ecstatic -p 80 -d test/serve",
        "status": "node test/status.js",
        "open": "opener http://localhost/relay.html && opener http://localhost/assert.html",
        "test": "npm run build && npm run serve && npm run status && npm run open"
    },
    "devDependencies": {
        "browserify": "",
        "ecstatic": "",
        "opener": "",
    }
}

Run tests in iframe

I can use smokestack if I run the tests in one web page... Maybe iframes will work...

Things that I should know before I switch:

  • window.addEventListener('storage', cb) must fire in an iframe, when the localStorage.setItem is called in a sibling iframe.
  • console.log should log to the console in an iframe. (Maybe not required)

If I do end up switching to iframes, see if travis will work. I might need to use phantom. If so, figure out how... (I think smokestack has an option for phantom, but it might need to be manually installed?)

The demo is broken

When you type something, half the time is doesn't work. Is the lib broken? Deprecation of synchronous APIs? Cuz I'm using Firefox?

When you open a second window, you see that the other window kinda "lag"s behind

I wonder if setTimeout(..., 100) would work.

Add a distribution build?

Right now if someone doesn't want to use browserify, they can get this module from wzrd.in.

Maybe I should just include a distribution build: tab-emitter.min.js.

Simplify non-browserify example in readme

Only put in the bare minimum code in there:

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

Add keywords

package.json line 10

  • crosstab
  • cross tab
  • bazaar
  • intercom
  • broadcast
  • ipc
  • rpc
  • pubsub
  • publish
  • subscribe
  • hub
  • messages
  • window
  • emitter
  • browser
  • browserify
  • communicate
  • communication
  • iwc

demo preview in readme

Make a gif and put it at the top of the readme, with a big ol' link to the demo site.

demo

This needs a sweet gh-pages demo!

Merge the two test files?

Since I'm dynamically creating an iframe, and adding a srcdoc attribute in /test/test.js, I might as well just drop all of /test/relay.js into that srcdoc string.

I think I could remove the --static option from the tests, and it would simplify the testing.

At that point, I could just take test.js and move it from /test/test.js to tab-emitter/test.js.

emits event in current tab

tab-emitter emits the event in every tab, including the sending tab. If possible, this should be an option.

Either way, this should be documented.

Demo: Cut down on assets

  • Right now I'm including the entire bootswatch.min.css file which is 25kb. If I remove the parts I'm not using (glyphicons, grid layout, buttons, navbar, indicators, progress bars, navs, containers, dialogs) and only leave the stuff I am (fonts, .form-control, .container) then it should be a lot smaller. It might be easiest to write my own css, copying the styles I use from chrome, or (unminified) bootstrap.css, rather than stripping the current css file down.
  • And probably remove .text-center from the <style> tag in the <head>, as it is built into bootstrap.
  • See if you can remove 1 of the 2 fonts that are downloaded. font-weight: 300; looks fine IMO.
  • Tangent: the input box should be styled with
    <input class="form-control" style="display: inline; width: 130px;">

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.