# autobus

> Bus for Node &amp; Browserify

Latest version **1.0.0** (published 2022-08-02) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-08-02 |
| First published | 2015-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | sprawl, surikat |
| Keywords | bus, browserify |

## Links

- npm: https://www.npmjs.com/package/autobus
- Repository: https://github.com/spralle/autobus
- Homepage: https://github.com/spralle/autobus#readme
- Issues: https://github.com/spralle/autobus/issues
- npm.io page: https://npm.io/package/autobus

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^4.3.4

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2022-08-02
- 0.1.6 — 2021-01-05
- 0.1.5 — 2021-01-04
- 0.1.4 — 2016-04-07
- 0.1.3 — 2015-07-14
- 0.1.2 — 2015-07-14
- 0.1.1 — 2015-07-03
- 0.1.0 — 2015-06-25
- 0.0.1 — 2015-02-17

## README

# autobus

Bus for Node &amp; Browserify

-   [Purpose](#purpose)
-   [Installation](#installation)
-   [Usage](#usage-example)
-   [Methods](#methods)
    -   [join](#joinchannel-listener)
        -   [Wildcard Matching](#wildcard-matching)
    -   [send](#sendchannel-message)
    -   [publish](#publishchannel-message)
    -   [leave](#leavechannel-listener)

## Purpose

Send messages between channels and add listener functions to receive messages.

## Installation

```shell
npm install autobus
```

## Usage Example

```typescript
import AutoBus from 'autobus';

interface ChatMessage {
    text: string;
    user: string;
    date: Date;
}

const displayMessage = (message: ChatMessage) => {
    const { text, user, date } = message;
    const formattedDate = date.toTimeString().split(' ')[0];
    console.log(`[${formattedDate}] ${user}: ${text}`);
};

class ChatSession {
    constructor(autoBus: AutoBus, user: string) {
        this.autoBus = autoBus;
        this.user = user;
        this.init();
    }

    init() {
        this.autoBus.join('chat.channel', this.onMessageReceived.bind(this));
    }

    onMessageReceived(message: ChatMessage) {
        if (message.user === this.user) {
            return;
        }
        displayMessage(message);
    }

    publishMessage(message: string) {
        const chatMessage: ChatMessage = {
            text: message,
            user: this.user,
            date: new Date()
        };
        this.autoBus.publish('chat.channel', chatMessage);
    }
}

const autoBus = new AutoBus();

const session1 = new ChatSession(autoBus, 'User1');
const session2 = new ChatSession(autoBus, 'User2');

session1.publishMessage("Hello I'm User1");
session2.publishMessage("Hello I'm User2");
```

#### Example output

```
[12:30:28] User1: Hello I'm User1
[12:30:28] User2: Hello I'm User2
```

## Methods

### join(channel, listener)

Adds listener to channel (creates channel if it doesn't exist)

```typescript
const autoBus = new AutoBus();

const consoleLogData = (data: any) => {
    console.log(data);
};

autoBus.join('example.channel', consoleLogData);
```

#### Wildcard Matching

Join channel with wildcard name matching

```typescript
autoBus.join('*.channel', consoleLogData);
```

### send(channel, message)

Sends message to one listener on the channel, if there is more than one listener on the channel the method throws an error.

```typescript
autoBus.send('example.channel', 'example message');
```

### publish(channel, message)

Sends message to all listeners on the channel.

```typescript
autoBus.publish('example.channel', 'example message');
```

### leave(channel, listener)

Remove listener from channel

```typescript
autoBus.leave('example.channel', consoleLogData);
```

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