# cross-channel

> Cross window messaging

Latest version **0.6.2** (published 2017-03-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install cross-channel
pnpm add cross-channel
yarn add cross-channel
bun add cross-channel
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.2 |
| Published | 2017-03-15 |
| First published | 2016-10-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Constantine Genchevsky |
| Maintainers | constgen |
| Keywords | communication, messaging, broadcast, channel, cross, cross-frame, cross-window, cross-tab, inter-process, ipc |

## Links

- npm: https://www.npmjs.com/package/cross-channel
- Repository: https://github.com/constgen/cross-channel
- Homepage: https://github.com/constgen/cross-channel#readme
- Issues: https://github.com/constgen/cross-channel/issues
- npm.io page: https://npm.io/package/cross-channel

## Dependencies (1)

- [es6-symbol](https://npm.io/package/es6-symbol.md) 3.1.0

## Recent versions

- 0.6.2 (latest) — 2017-03-15
- 0.6.1 — 2017-02-11
- 0.6.0 — 2017-02-11
- 0.4.1 — 2016-10-22
- 0.4.0 — 2016-10-16

## README

# CrossChannel

Cross origin messaging channel. Sends messages between different JavaScript contexts.

## Installation

In a **Browser** environment:

```html
<script src="path/to/connexionjs/dist/cross-channel.min.js"></script>
<script>
	var channel = new CrossChannel('name')
	channel.on('message', handler)
	channel.postMessage(message)
</script>
```

In a **CommonJS** environment first install it from NPM:

```
$ npm install cross-channel
```

then in the source code use

```js
var CrossChannel = require('cross-channel')
var channel = new CrossChannel('name')
channel.on('message', handler)
channel.postMessage(message)
```

## Reference

### `channel.name: String`

The name of a channel

### `channel.postMessage(message: Object)`

Sends a message to all other channel instances with the same name. Message can't be a DOM element or contain a DOM element. The message is cloned between different execution contexts.

### `channel.on('message', handler: Function)`

Attaches listener to a 'message' event. The handler is called only when message is sent from another instances with the same channel name, but not called when `postmessage()` is called on the same instance. The callback is executed with a single argument of MessageEvent object. The **event** interface:

```
MessageEvent {
	type: String
	data: Object|Boolean|String|Number|Null|Undefined
	timeStamp: Number
	origin: String
	sourceChannel: String
}
```

### `channel.once('message', handler: Function)`

Attaches listener that is executed only once for the very first 'message' event.

### `channel.addEventListener('message', handler: Function)`

The alias to `channel.on()`. Necessary for a compatibility with **BroadcastChannel API** when used as a polifyll.

### `channel.removeEventListener('message', handler: Function)`

Removes an event handler of a 'message' event.

### `channel.removeAllListeners('message')`

Removes all event handlers of a 'message' event.

### `channel.close()`

Removes all channel event handlers and closes a channel to reveal memory.

## Examle

```js
var channelGreen1 = new CrossChannel('green')
var channelGreen2 = new CrossChannel('green')

channelGreen1.postMessage({text: 'message'})
channelGreen2.on('message', function(event){
	console.log(event.data) // -> {text: 'message'}
})
```

## Use cases:
- need a **BroadcastChannel** polifyll;
- developement of a multiple screen expirience in a browser;
- development of a JavaScript API for media players that are embedded in an `<iframe>`;
- need an unified messaging channel in browser extensions;
- messaging between all frames with any origins in a browser window;
- messaging between all windows/frames and background processes on NWJS platform;
- messaging between "Node" and "Webkit" context on the Node-webkit platform;
- messaging across tabs of the same origin
<!--- messaging between different main process and child processes in NodeJS-->

## Compatibility
<!-- http://www.tablesgenerator.com/markdown_tables -->
### Browsers
| Browsers             |  self  | frames  | tabs |
|----------------------|:------:|:-------:|:----:|
| Firefox              |    ✓   |   ✓    |   ✓  |
| Android Firefox      |    ✓   |   ✓    |   ✓  |
| Chrome               |    ✓   |   ✓    |   ✓  |
| Android Chrome       |    ✓   |   ✓    |   ✓  |
| Opera <=9            |    -   |   -     |   -  |
| Opera <=12           |    ✓   |   ✓    |   ✓  |
| Opera >=15           |    ✓   |   ✓    |   ✓  |
| Android Opera >=15   |    ✓   |   ✓    |   ✓  |
| Opera mini           |    ✓   |   ✓    |   ✓  |
| Safari >=5           |    ✓   |   ✓    |   ✓  |
| Safari on iOS >= 8   |    ✓   |   ✓    |   ✓  |
<!--| Chrome on iOS        |    ✓   |   ✓    |   -  |-->
<!--| Firefox on iOS       |    ✓   |   ✓    |   -  |-->
| Android Browser >=4.1|    ✓   |   ✓    |   ✓  |
<!--| IE <=8               |    -   |   -     |   -  |-->
| IE >=9               |    ✓   |   ✓    |  ~   |
| Mobile IE >=10       |    ✓   |   ✓    |   -  |
| Edge                 |    ✓   |   ✓    |   ✓  |
| Worker               |        |         |      |

### Extensions
| Browsers             |  self  | background  | content | popup | options |
|----------------------|:------:|:-----------:|:-------:|:-----:|:-------:|
| Chrome extension     |    ✓   |      ✓     |    ✓    |   ✓   |    ✓   |
| Firefox WebExtension |    ✓   |      ✓     |    ✓    |   ✓   |    ✓   |
| Edge WebExtension    |        |             |         |       |         |
| Safari extension     |    -   |      -      |    -    |   -   |    -    |

### Applications
| Platforms            |  self  | frames | tabs | webview | background | NodeJS |
|----------------------|:------:|:------:|:----:|:-------:|:----------:|:------:| 
| Node-webkit <=0.11   |    ✓   |   ✓    |   -  |    o    |     o     |   ✓    |
| NWJS >=0.13          |    ✓   |   ✓    |   ✓  |         |     ✓     |   -    |
| Electron             |        |        |      |         |            |        |
| Chrome app           |        |        |      |         |            |        |
| Windows Universal app|        |        |      |         |            |        |
| Cordova              |        |        |      |         |            |        |
| Firefox OS app       |        |        |      |         |            |        |

### Server side
| Platforms            |  self  | children | parallel | forks |
|----------------------|:------:|:--------:|:--------:|:-----:|
| NodeJS               |    -   |     -    |     -    |   -   |

<!--## Polyfills that may be required for old platforms:
- Object.create() (IE <=8, FF <=3.6, SF <5, iOS <=5.1, CH <6, OP <=11.50)-->

---
_Source: https://npm.io/package/cross-channel · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
