# ubus

> Micro message bus for the browser.

Latest version **1.0.13** (published 2019-11-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install ubus
pnpm add ubus
yarn add ubus
bun add ubus
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.13 |
| Published | 2019-11-25 |
| First published | 2016-08-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 19 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Eric Mendes Dantas |
| Maintainers | ericmdantas |
| Keywords | message, bus, event-emitter, event, javascript, browser, micro, small, client, es2015, commonjs, simple, light |

## Links

- npm: https://www.npmjs.com/package/ubus
- Repository: https://github.com/ericmdantas/uBus
- Homepage: https://github.com/ericmdantas/uBus#readme
- Issues: https://github.com/ericmdantas/uBus/issues
- npm.io page: https://npm.io/package/ubus

## Dependencies (2)

- [babel-cli](https://npm.io/package/babel-cli.md) ^6.26.0
- [babel-preset-env](https://npm.io/package/babel-preset-env.md) ^1.7.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.13 (latest) — 2019-11-25
- 1.0.12 — 2019-07-19
- 1.0.11 — 2019-05-24
- 1.0.10 — 2019-05-24
- 1.0.9 — 2019-04-04
- 1.0.8 — 2019-04-01
- 1.0.7 — 2019-03-29
- 1.0.4 — 2016-10-21
- 1.0.3 — 2016-09-18
- 1.0.2 — 2016-09-14
- 1.0.1 — 2016-09-14
- 1.0.0 — 2016-09-12
- 0.0.14 — 2016-09-05
- 0.0.13 — 2016-09-03
- 0.0.12 — 2016-09-01
- … 11 more at https://npm.io/package/ubus/versions

## README

# μBus

[![Build Status](https://travis-ci.org/ericmdantas/uBus.svg?branch=master)](https://travis-ci.org/ericmdantas/uBus)
[![npm version](https://badge.fury.io/js/ubus.svg)](https://badge.fury.io/js/ubus)


### Install

```shell
$ npm i --save ubus
```


### What is this?

μBus (micro bus) is a micro implementation of a message bus/event emitter in javascript. It was created to be light, fast and intuitive - it's less than `500 bytes` when gzipped *(es2015 module)*.

The API is minimal and straight forward. There's `one` class and `four` methods - nothing more, nothing less.

You can use it anywhere javascript is being used, both server and client side.

Also, given there are a lot of different projects out there, `μBus` has `five` different `module loaders` for you to play with:

- `es2015`;
- `commonjs`; 
- `systemjs`;
- `amd`;
- `umd`. 

Pick the one that fits your project and have fun!


### API

#### on

```ts
  bus.on(token: string, callback: Function): () => void
```

```js
  const {Bus} = require('ubus');
  let bus = new Bus();

  bus.on('my-event', () => {
    console.log('yo!'); // logs 'yo!' every time this Bus instance emits 'my-event'
  });

  bus.on('my-other-event', (info) => {
    console.log(info); // logs info every time this Bus instance emits 'my-other-event'
  });
```

#### once

```ts
  bus.once(token: string, callback: Function):void
```

```js
  const {Bus} = require('ubus');
  let bus = new Bus();

  bus.once('my-event', () => {
    console.log('yo!'); // logs 'yo!' only once per bus instance
  });

  bus.once('my-other-event', (info) => {
    console.log(info); // logs 'yo!' only once per bus instance
  });
```


#### emit

```ts
  bus.emit(token: string, info?: any):void
```

```js
  const {Bus} = require('ubus');
  let bus = new Bus();

  bus.on('my-event', () => {
    console.log('yo!'); // logs 'yo!' when 'my-event' is called
  });

  bus.on('my-other-event', (info) => {
    console.log(info); // logs { yo: true } when 'my-other-event' is called
  });

  bus.emit('my-event');
  bus.emit('my-other-event', { yo: true });
```

#### off

```ts
  bus.off(token: string | string[]):void
```

```js
  const {Bus} = require('ubus');
  let bus = new Bus();

  bus.on('my-event', () => {
    console.log('yo!'); // logs 'yo!' when 'my-event' is emitted
  });

  bus.on('my-other-event', (info) => {
    console.log(info); // logs { yo: true } when 'my-other-event' is emitted
  });


  bus.emit('my-event'); // will trigger the event
  bus.emit('my-other-event', { yo: true }); // will trigger the event

  bus.off('my-event');
  bus.off('my-other-event');

  /* or
  bus.off([
    'my-event',
    'my-other-event'
  ]);
  */

  bus.emit('my-event'); // won't trigger the event, nobody listening
  bus.emit('my-other-event', { yo: true }); // won't trigger the event, nobody listening
```


#### destroy function

```ts
  let destroyFn = bus.on(token: string, cb: Function): () => void
```

```js
  const {Bus} = require('ubus');
  let bus = new Bus();

  let _destroyMyEvent = bus.on('my-event', () => {
    console.log('yo!'); // logs 'yo!' when 'my-event' is called
  });

  let _destroyMyOtherEvent = bus.on('my-other-event', (info) => {
    console.log(info); // logs { yo: true } when 'my-other-event' is called
  });

  bus.emit('my-event'); // triggers the event
  bus.emit('my-other-event', { yo: true }); // triggers the event

  _destroyMyEvent(); // destroys 'my-event'
  _destroyMyOtherEvent(); // destroys 'my-other-event'

  bus.emit('my-event'); // triggers nothing, no one listening
  bus.emit('my-other-event', { yo: true }); // triggers nothing, no one listening
```

### Wiki

For more information on how to integrate with existing projects, Benchmarks, FAQ, Troubleshooting and other stuff, take a look at the [wiki](https://github.com/ericmdantas/uBus/wiki).


### Inspired by

- [Node.js](https://github.com/nodejs/node)'s EventEmitter.
- [socket.io](https://github.com/socketio/socket.io)'s simple API;
- [angular (1.x)](https://github.com/angular/angular.js)'s implementation of `$emit/$broadcast` and `$on`;
- [minibus](https://github.com/axelpale/minibus)'s compact API.


### LICENSE

MIT

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