• Stars
    star
    219
  • Rank 181,133 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-tab message bus for browsers.

tabex

Build Status NPM version

Cross-tab message bus for browsers.

Awesome things to do with this library:

  • Send messages between browser tabs and windows.
  • Share single websocket connection when multiple tabs open (save server resources).
  • Shared locks (run live sound notification only in single tab on server event).

Demo

Supported browser:

  • All modern and IE9+.
  • Ancient browsers will fallback to legacy mode:
    • Each tab will not know about neighbours.
    • Clients in the same window will be ok.

Known issues:

  • Safari in private mode will fallback to legacy, because it prohibits localStorage write.
  • Chrome on iOS will fallback to legacy, because it does not send localStorage events between tabs.
  • Cross-domain messaging is not recommended due serious unfixable problems in browsers:
    • will not work in Safari by default, because of security settings.
    • will not work in IE11 due multiple bugs.

Install

node.js (for use with browserify):

$ npm install tabex

bower:

$ bower install tabex

API

In 99% of use cases your tabs are on the same domain. Then tabex use is very simple:

// Do it in every browser tab, and clients become magically connected
// to common broadcast bus :)
var live = window.tabex.client();

live.on('channel.name', function handler(message) {
  // Do something
});

// Broadcast message to subscribed clients in all tabs, except self.
live.emit('channel.name2', message);

// Broadcast message to subscribed clients in all tabs, including self.
live.emit('channel.name2', message, true);

Client

tabex.client(options)

Fabric to create messaging interface. For single domain you don't need any options, and everything will be initialized automatically. For cross-domain communication you will have to create html file with router for iframe, loaded from shared domain.

Options:

  • iframe (String) - url of iframe source with router (on shared domain). Optional.
  • namespace (String) - optional, for mad case when you need muiltiple isolated tabex instances in parallel.

client.on(channel, handler)

Subscribe to channel. Chainable.

  • channel (String) - name of channel to subscribe.
  • handler (Function) - function (data, channel) { ... }.

client.off(channel [, handler])

Unsubscribe handler from channel. Chainable. If handler not specified - unsubscribe all handlers from given channel.

client.emit(channel, message [, toSelf])

Send message to all tabs in specified channel. By default messages are broadcasted to all clients except current one. To include existing client - set toSelf to true.

client.lock(id, [timeout, ] fn):

  • id - lock identifier
  • timeout - optional, lock lifetime in ms, default 5000
  • fn(unlock) - handler will be executed if lock is acquired
    • unlock - function to release acquired lock

Note. Getting lock takes 30ms when localStorage used as events transport.

client.filterIn(fn), client.filterOut(fn)

Add transformers for incoming (handled) and outgoing (emitted) messages. Chainable. This is a very powerful feature, allowing tabex customization. You can morph data as you wish when it pass client pipeline from one end to another.

  • fn(channel, message, callback) - filter function.
    • channel - channel name.
    • message - wrapped event data (see below).
    • callback(channel, message) - function to return output data.

Filter message structure:

  • id - unique message id, <node_id>_<msg_counter>. Consists or unique client instance id (random) and message ccounter (inremented for each new message)
  • node_id - id message emitter source (client or router), random string.
  • data - message data, passed to client.emit().

Use case examples:

  • faye does not allow . in channel names. You can add filters for transparent replace of . with !!.
  • you can drop and generate internal events to implement new features like locks or remote function calls.

Router

Direct access to router is needed only for cross-domain communication (with iframe). For single domain tabex client will create local router automatically.

tabex.router(options)

Fabric to create router in iframe. Options:

  • origin (Array|String) - list of valid origins, allowed to communicate with this iframe. If nothing set, then iframe domain will be used.
  • namespace - the same as in tabex.client.

Example.

In your code:

var live = window.tabex.client({
  iframe: 'https://shared.yourdomain.com/tabex_iframe.html'
});

In iframe:

window.tabex.router({
  origin: [
    'http://yourdomain.com',
    'https://yourdomain.com',
    'http://forum.yourdomain.com',
    'https://forum.yourdomain.com'
  ]
});

Warning! Never set * to allowed origins value. That's not secure.

Advanced use

System events

Channels !sys.* are reserved for internal needs and extensions. Also tabex already has built-in events, emitted on some state changes:

  • !sys.channels.refresh - emitted when list of subscribed channels changed (or in specific case, when master tab switched). Message data:
    • channels - array of all channels subscribed in all tabs
  • !sys.channels.add - emitted by tabex.client to notify router about new subscribed channel. Message data:
    • channel - channel name
  • !sys.channels.remove - emitted by tabex.client to notify router that all subscriptions to channel gone. Message data:
    • channel - channel name
  • !sys.lock.request - emitted by tabex.client to try acquire lock. Message data:
    • id - lock identifier
    • timeout - lock lifetime in ms
  • !sys.lock.acquired - emitted when router acquire lock for client
    • request_id - request message id
  • !sys.lock.release - emitted by tabex.client to release already acquired lock. Message data:
    • id - lock identifier
  • !sys.error - emitted on internal errors, for debug.
  • !sys.master - sepecific for localStorage-based router. Message data:
    • node_id - id of "local" router node
    • master_id - id of node that become master

Note. !sys.master event is broadcasted only when localStorage router used. You should NOT rely on it in your general application logic. Use locks instead to filter single handler on broadcasts.

Sharing single server connection (faye)

Example below shows how to extend tabex to share single faye connection between all open tab. We will create faye instances it all tabs, but activate only one in "master" tab.

User can close tab with active server connection. When this happens, new master will be elected and new faye instance will be activated.

We also do "transparent" subscribe to faye channels when user subscribes with tabex client. Since user can wish to do local broadcasts too, strict separation required for "local" and "remote". We do it with addind "remote.*" prefix for channels which require server subscribtions.

Note. If you don't need cross-domain features - drop iframe-related options and code.

In iframe:

window.tabex.router({
  origin: [ '*://*.yourdomain.com', '*://yourdomain.com' ]
});

In client:

// This one is for your application.
//
var live = window.tabex({
  iframe: 'https://shared.yourdomain.com/tabex_iframe.html'
});


// Faye will work via separate interface. Second interface instance will
// reuse the same router automatically. Always use separate interface for
// different bus build blocks to properly track message sources in filters.
//
// Note, you can attach faye in iframe, but it's more convenient to keep
// iframe source simple. `tabex` is isomorphic, and faye code location does
// not make sense.
//
var flive = window.tabex({
  iframe: 'https://shared.yourdomain.com/tabex_iframe.html'
});


var fayeClient = null;
var trackedChannels = {};


// Connect to messaging server when become master and
// kill connection if master changed
//
flive.on('!sys.master', function (data) {

  // If new master is in our tab - connect
  if (data.node_id === data.master_id) {
    if (!fayeClient) {
      fayeClient = new window.faye.Client('/faye-server');
    }
    return;
  }

  // If new master is in another tab - make sure to destroy zombie connection.
  if (fayeClient) {
    fayeClient.disconnect();
    fayeClient = null;
    trackedChannels = {};
  }
});


// If list of active channels changed - subscribe to new channels and
// remove outdated ones.
//
flive.on('!sys.channels.refresh', function (data) {

  if (!fayeClient) {
    return;
  }

  // Filter channels by prefix `local.` and system channels (starts with `!sys.`)
  var channels = data.channels.filter(function (channel) {
    return channel.indexOf('local.') !== 0 && channel.indexOf('!sys.') !== 0;
  });

  // Unsubscribe removed channels
  //
  Object.keys(trackedChannels).forEach(function (channel) {
    if (data.channels.indexOf(channel) === -1) {
      trackedChannels[channel].cancel();
      delete trackedChannels[channel];
    }
  });

  // Subscribe to new channels
  //
  data.channels.forEach(function (channel) {
    if (!trackedChannels.hasOwnProperty(channel)) {
      trackedChannels[channel] = fayeClient.subscribe('/' + channel.replace(/\./g, '!!'), function (message) {
        flive.emit(channel, message.data);
      });
    }
  });
});


// Resend events without prefix `local.` and prefix `!sys` to server, convert channel
// names to faye-compatible format: add '/' at start of channel name and replace '.' with '!!'
//
flive.filterIn(function (channel, message, callback) {
  if (fayeClient && channel.indexOf('local.') !== 0 && channel.indexOf('!sys.') !== 0) {
    fayeClient.publish('/' + channel.replace(/\./g, '!!'), message);
    return;
  }

  callback(channel, message);
});


// Convert channel name back from faye compatible format: remove '/'
// at start of channel name and replace '!!' with '.'
//
flive.filterOut(function (channel, message, callback) {
  if (channel[0] === '/') {
    callback(channel.slice(1).replace(/!!/g, '.'), message);
    return;
  }

  callback(channel, message);
});

License

MIT

More Repositories

1

js-yaml

JavaScript YAML parser and dumper. Very fast.
JavaScript
6,289
star
2

pako

high speed zlib port to javascript, works in browser & node.js
JavaScript
5,550
star
3

pica

Resize image in browser with high quality and high speed
JavaScript
3,759
star
4

probe-image-size

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.
JavaScript
984
star
5

argparse

CLI arguments parser for node.js. JS port of python's argparse module.
JavaScript
489
star
6

image-blob-reduce

Resize image blobs with high quality. Pica's wrapper to work with file inputs.
JavaScript
272
star
7

babelfish

human friendly i18n for javascript (node.js + browser)
JavaScript
255
star
8

url-unshort

Short links expander for node.js
JavaScript
115
star
9

ndoc

js port of pdoc, with extentions
JavaScript
101
star
10

charlatan

Fake identities generator for node.js (names, addresses, phones, IPs and others). Supports multiple languages.
JavaScript
96
star
11

nodeca

Forums / Blogs / Groups / Classfields / ... platform. Fork this to make your own config.
JavaScript
96
star
12

bag.js

JS / CSS / files loader + key/value storage
JavaScript
90
star
13

idoit

Redis-backed task queue engine with advanced task control and eventual consistency
JavaScript
74
star
14

glur

Fast gaussian blur in pure JavaScript via IIR filer
JavaScript
72
star
15

multimath

WebAssembly wrapper to simplify fast math coding
JavaScript
70
star
16

embedza

Create HTML snippets/embeds from URLs using info from oEmbed, Open Graph, meta tags.
JavaScript
64
star
17

promise-memoize

Memoize promise-returning functions. Includes cache expire and prefetch.
JavaScript
59
star
18

navit

Simple client testing from your scripts
JavaScript
48
star
19

unhomoglyph

Replace all homoglyphs with base characters. Useful to detect similar strings.
JavaScript
39
star
20

nntp-server

NNTP server for readers
JavaScript
27
star
21

puncher

Library to set timestamps in your application requests & genegate profiling tree.
JavaScript
14
star
22

eslint-plugin-nodeca

Indentation check rule for ESLint
JavaScript
13
star
23

nodeca.core

Nodeca core app (admin panel, loader, bundler)
JavaScript
12
star
24

types

Collection of extra types (structures, classes) for JavaScript.
JavaScript
12
star
25

nodeca.users

Nodeca user app (login / register / albums / profiles)
JavaScript
11
star
26

event-wire

Mediator with dynamic responsibility chains
JavaScript
10
star
27

hike-js

Javascript port of Hike (Ruby) - a library for finding files in a set of paths
JavaScript
10
star
28

plurals-cldr

Plurals suport for JS, autogenerated from CLDR.
JavaScript
9
star
29

relimit

Rate limiter with tuneable scheduler and distributed run support
JavaScript
9
star
30

js-yaml-js-types

Extra js types for js-yaml
JavaScript
8
star
31

mimoza

Simple mime-type tools library
JavaScript
8
star
32

pointer

router for node.js / client
JavaScript
7
star
33

nodeca.forum

Nodeca forum app
JavaScript
4
star
34

redis-if

JavaScript
4
star
35

nodeca-design

Nodeca interface mockups
HTML
4
star
36

fontomas

3
star
37

nodeca.blogs

Blogs component for Nodeca
JavaScript
3
star
38

nodeca.market

Market component for Nodeca
JavaScript
2
star
39

nodeca.clubs

Clubs component for Nodeca
JavaScript
2
star
40

charcount

Count visual length of javascript string
JavaScript
2
star
41

bkv

JavaScript
1
star
42

nodeca.nntp

JavaScript
1
star
43

nodeca.vbconvert

vBulletin -> Nodeca convertor
JavaScript
1
star
44

nodeca-doc

Nodeca internals documentation
CSS
1
star
45

nodeca.search

JavaScript
1
star
46

.github

1
star
47

nodeca.site

Custom configuration for nodeca
JavaScript
1
star