# channel-bus

> Simple bus-style state management for real-time API bridges

Latest version **1.0.1** (published 2020-04-21) · MIT license · 0 weekly downloads

## Install

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

## 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.1 |
| Published | 2020-04-21 |
| First published | 2020-04-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sebastian Walker |
| Maintainers | sebastianwalker |
| Keywords | state, framework, bus, realtime, api, bridge |

## Links

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

## Dependencies (1)

- [on-change](https://npm.io/package/on-change.md) ^2.0.1

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2020-04-21
- 1.0.0 — 2020-04-21

## README

# channel-bus

So, you want to bodge together different APIs in real-time with Node.Js?

- State needs to be shared across different components in real-time.
- Components need to react to state changes.
- Components perform actions asynchronously.
- Components need to poll data in regular intervals.

**Then, channel-bus is your perfect companion**. I use it all the time to hook together
Websockets, REST APIs, Arduinos, Joysticks, Websockets and many more things.

## What are channels?
Each channel represents a single concern of your application. Channels can read and write the global state freely
and at any time.  
For example, one channel may be responsible for storing incoming data from a serial port to the global state while
another broadcasts this information to a webhook.

Now, for example, if you also need the information pushed to a REST api, you may simply add another channel for that!

## Example channel

```js
const Channel = require('channel-bus').Channel;

class SampleChannel extends Channel {
    /*
    Assign a user-friendly, descriptive name for this channel.
    */
    get name(){
        return "Sample Channel";
    }

    /*
    Register event listeners, open connections, setup libraries, etc.
    Called once on launch.
     */
    init(){
        //
        this.log("Hello world!")
    }

    /*
    Poll data sources, etc
    Called regularly at the configured interval (if tick is enabled)
     */
    tick(){
        //
    }

    /*
    Push changed state to external services, calculate results, etc.
    Called every time the state object is changed (deeply watched)
     */
    stateChange(){
        //
    }
}

module.exports = SampleChannel;
```

## Example bus

```js
const Bus = require('channel-bus').Bus;

const bus = new Bus();

bus
    // Use this array of channel instances
    .withChannels([
        new (require('./exampleChannel'))
    ])

    // Use the following global config
    .withConfig({
        websocketHost: 'localhost',
        websocketPort: 8080,
    })

    // Enable tick every second
    .enableTick(1000)

    // Initialize with provided global state
    .initWith({
        websocketConnected: false,
        messages: []
    });

```

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