# simple-message-channels

> Simple streamable state machine that implements a useful channel, message-type, message pattern

Latest version **1.2.1** (published 2019-10-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-message-channels
pnpm add simple-message-channels
yarn add simple-message-channels
bun add simple-message-channels
```

## 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.2.1 |
| Published | 2019-10-09 |
| First published | 2019-08-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 39 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/simple-message-channels
- Repository: https://github.com/mafintosh/simple-message-channels
- Issues: https://github.com/mafintosh/simple-message-channels/issues
- npm.io page: https://npm.io/package/simple-message-channels

## Dependencies (1)

- [varint](https://npm.io/package/varint.md) ^5.0.0

## Recent versions

- 1.2.1 (latest) — 2019-10-09
- 1.2.0 — 2019-10-09
- 1.1.3 — 2019-10-08
- 1.1.2 — 2019-09-06
- 1.1.1 — 2019-08-29
- 1.1.0 — 2019-08-29
- 1.0.0 — 2019-08-09
- 0.0.0 — 2019-08-09

## README

# simple-message-channels

Simple streamable state machine that implements a useful channel, message-type, message pattern

```
npm install simple-message-channels
```

[![Build Status](https://travis-ci.org/mafintosh/simple-message-channels.svg?branch=master)](https://travis-ci.org/mafintosh/simple-message-channels)

## Usage

``` js
const SMC = require('simple-message-channels')

const a = new SMC({
  onmessage (channel, type, message) {
    console.log('Received a message on channel', channel) // a number
    console.log('Message type is', type) // a number
    console.log('And the message payload was', message) // a buffer
  }
})

const b = new SMC()

// produce a payload
const payload = b.send(0, 1, Buffer.from('hi'))

// somehow send it to the other instance (over a stream etc)
// can arrive chunked as long as it arrives in order
a.recv(payload)
```

(This is the basic wire protocol used by hypercore, https://github.com/mafintosh/hypercore)

## API

#### `payloadBuffer = smc.send(channel, type, message)`

Encode a channel, type, message to be sent to another person.
Channel can be any number and type can be any 4 bit number.
Message should be a buffer.

#### `success = smc.recv(payloadChunk)`

Parse a payload buffer chunk. Once a full message has been parsed
the `smc.onmessage(channel, type, message)` handler is called.

Returns true if the chunk seemed valid and false if not.
If false is returned check `smc.error` to see the error it hit.

#### `payloadBuffer = smc.sendBatch([{ channel, type, message }, ...])`

Encodes a series of messages into a single paylaod buffer.

## License

MIT

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