# msg-pubsub

> msg-pubsub is a simple events dispatcher publish / subscribe library (just 0.9kb). aysnc, simple and usefull.

Latest version **0.2.0** (published 2018-02-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install msg-pubsub
pnpm add msg-pubsub
yarn add msg-pubsub
bun add msg-pubsub
```

## 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.2.0 |
| Published | 2018-02-01 |
| First published | 2016-08-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | muwenzi |
| Maintainers | kimigao |
| Keywords | msg-pubsub, event publish, event subscribe, dispatcher, react, vue.js, angular, component communication |

## Links

- npm: https://www.npmjs.com/package/msg-pubsub
- Repository: https://github.com/muwenzi/msg-pubsub
- Homepage: https://github.com/muwenzi/msg-pubsub#readme
- Issues: https://github.com/muwenzi/msg-pubsub/issues
- npm.io page: https://npm.io/package/msg-pubsub

## 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

- 0.2.0 (latest) — 2018-02-01
- 0.1.3 — 2016-08-24
- 0.1.2 — 2016-08-23
- 0.1.1 — 2016-08-22
- 0.1.0 — 2016-08-22

## README

# msg-pubsub

[![build status][travis-image]][travis-url] [![npm version][npm-version-image]][npm-version-url] [![npm downloads][npm-downloads-image]][npm-downloads-url] [![license][license-image]][license-url]

English | [简体中文][zh-CN-url]

> **msg-pubsub** is a simple js lib (only `0.9kb`) for dispatching message by publish/subscribe pattern.

Can be used in:

- simple message/events dispatcher.
- Cross-component communication of React / Vue / Angular.
- message subscribe and publish.

## API Methods

**1. `pub(msg_name, data1, data2, ...)`**

Async subscribe the message named `msg_name`, and with `datas` as the input of `callback` function.

**2. `pubSync(msg_name, data1, data2, ...)`**

Sync subscribe the message named `msg_name`, and with `datas` as the input of `callback` function, blocking.

**3. `sub(msg_name, callback, context)`**

Subscribe the `msg_name` with `callback`. `callback` function will execute when the message named `msg_name` published.

This function will return the `msgObj` which can be used to cancel bind with the API `un(msgObj)`.

**4. `subOnce(msg_name, callback, context)`**

Subscribe the `msg_name` with `callback`. It will be expired once Ttriggered.

Only can be triggered once, then it will be deleted automation.

**5. `unsub(msgObj / msg_name)`**

Cancel subscribe message. You can unsubscribe a message Object, or just unsubscribe a `msg_name`, or unsubscribe the `callback` function.

**6. `clear()`**

Clear all message/events.

## Example

### 1. Import library

```sh
npm i msg-pubsub
```

You can import it with `<script>` tag, or use keyword `require` or `import`.

```js
import msg from 'msg-pubsub';

// or

var msg = require("msg-pubsub");
```

### 2. Simple usage

Use `pub` method to publish message, use `sub` method to subscribe message, `unsub` to unsubscribe.

```js
import msg from 'msg-pubsub';

function test_callback(data1, data2) {
  console.log('this is msg 1');
}

// publish message
msg.pub('test_msg', 'test_data1'， 'test_data2');

// subscribe message
var msgObj = msg.sub('test_msg', test_callback);
var msgObj2 = msg.sub('test_msg', function(data1, data2) {
  console.log('this is msg 2');
});

// cancel message
msg.unsub(msgObj); // cancel message
msg.unsub('test_msg'); // cancel all messages called `test_msg`.
msg.unsub(test_callback); // cancel all `test_callback` methods.
```

## License

[MIT](license-url)

[travis-url]: httmsg://travis-ci.org/muwenzi/msg-pubsub
[travis-image]: https://img.shields.io/travis/muwenzi/msg-pubsub/master.svg?style=flat-square

[npm-version-url]: https://www.npmjs.com/package/msg-pubsub
[npm-version-image]: https://img.shields.io/npm/v/msg-pubsub.svg?style=flat-square

[npm-downloads-url]: https://www.npmjs.com/package/msg-pubsub
[npm-downloads-image]: https://img.shields.io/npm/dt/msg-pubsub.svg?style=flat-square

[license-url]: https://github.com/muwenzi/msg-pubsub/blob/master/LICENSE
[license-image]: https://img.shields.io/github/license/muwenzi/msg-pubsub.svg?style=flat-square

[zh-CN-url]: https://github.com/muwenzi/msg-pubsub/blob/master/README.zh-CN.md

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