# obs-bus

> A simple message bus implementation

Latest version **1.0.5** (published 2021-09-05) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install obs-bus
pnpm add obs-bus
yarn add obs-bus
bun add obs-bus
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2021-09-05 |
| First published | 2021-05-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 31.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Konstantin Rybakov |
| Maintainers | viocost |

## Links

- npm: https://www.npmjs.com/package/obs-bus
- Repository: https://github.com/viocost/obs-bus
- npm.io page: https://npm.io/package/obs-bus

## Dependencies (1)

- [cute-set](https://npm.io/package/cute-set.md) ^2.0.1

## Recent versions

- 1.0.5 (latest) — 2021-09-05
- 1.0.4 — 2021-08-29
- 1.0.3 — 2021-08-24
- 1.0.2 — 2021-08-24
- 1.0.1 — 2021-08-24
- 1.0.0 — 2021-08-24
- 0.0.4 — 2021-06-09
- 0.0.3 — 2021-06-02
- 0.0.1 — 2021-05-18

## README

Observer pattern message bus

# Motivation
Most of event driven libraries implement following pattern:

```
emitter.on("some_event", some_callback)
```
While it is certainly useful and solves coupling problem, there are still few issues with it.
We have to explicitly introduce callbacks to an emitter object, thus coupling both together.
Such pattern encourages to make huge objects with tons of event handlers that are hard to maintain.

# obs-bus 
This library offers different approach.

``` javascript
import MessageBus from "obs-bus"

// Initializing message bus
const mBus = new MessageBus()

class Subscriber{
    //Any subscriber must have update method.
    //Following parameters will be passed when update called:
    update(message, senderObject, messageBus){
        //process message
    }
}

// Subscribing an object to a message
mBus.subscribe({subscriber, message, channel}) 

//We can form a message as array where first item is 
//the name of the message and other items is data
const message1 = ['TEST', {/*some data*/}]

//We can just send a string without any data.
//It will be turned into ['HEY'] implicitly.
const message2 = 'HEY'


// Putting message on the bus
mBus.deliver(message1, senderObject, channel)

// Now if message1 is relevant to subscriber, then subscriber's update method will be called.

```

So, if we want obj1 and obj2 to communicate, we don't have to 
make one of them some kind of emitter and have the other share some callback explicitly. We just subscribe them to message bus and they talk to each other by sending messages.


# Subscriptions 

## Subscribing

``` javascript
mBus.subscribe({subscriber, message: "message_type_as_string", channel: '55'})
```
message and channel are optional
message can be any string or symbol, channel can be anything subscriber must have an update method that is called when relevant message is received,  otherwise error will be thrown.


A subscriber can subscribe to all messages:
```javascript
mBus.subscribe({subscriber})

```

// Subscriber can subscribe to certain type of messages
```javascript
mBus.subscribe({subscriber, message: "message_type_as_string"})
```

Subscriber can subscribe to a message and a channel.
Message will be delivered to the subscriber only when message type
channel match.
```javascript
mBus.subscribe({subscriber, message: "message_type_as_string", channel: '55'})
```

Subscriber can subscribe to a channel, thus
any message sent over to channel '55' will be delivered to subscriber
```javascript
mBus.subscribe({subscriber,  channel: '55'})
```

## Channels
Channel can be anything: number, string, object, array or another sender. 

Each sender represents its own channel. When messages are delivered and there are some objects subscribed to the sender of the message - the message will be delivered while channel not explicitly provided: 


```javascript
const obj1, obj2
mBus.subscribe({obj1,  channel: obj2})

mBus.deliver(some_message, obj2)
// obj1 will receive this message
```

## Unsubscribing
We can unsubscribe objects completely, no more messages will be delivered to this obj:
```javascript
mBus.unsubscribe(obj)
```

We can unsubscribe from message or channel:
```javascript
mBus.unsubscribe(obj, {message: "message_type"})
mBus.unsubscribe(obj, {channel})
```

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