simple-message-channels
Simple streamable state machine that implements a useful channel, message-type, message pattern
Last updated 5 years ago by mafintosh .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install simple-message-channels 
SYNC missed versions from official npm registry.

simple-message-channels

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

npm install simple-message-channels

Build Status

Usage

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

Current Tags

  • 1.2.1                                ...           latest (5 years ago)

8 Versions

  • 1.2.1                                ...           5 years ago
  • 1.2.0                                ...           5 years ago
  • 1.1.3                                ...           5 years ago
  • 1.1.2                                ...           5 years ago
  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (1)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |