msgp

1.0.0 • Public • Published

MSGP

Byte message protocol and streaming parser for Node.js.

NPM version Build Status Downloads

Rust version: https://github.com/iorust/msgp-rust

Protocol

Byte message protocol

Bench

1000000 simple messages, amp vs msgp vs resp

  1. Mspg: 23437.50 kb 661813.37 ops
  2. resp: 29296.88 kb 646412.41 ops
  3. Amp: 27343.75 kb 222419.93 ops

API

const Msgp = require('msgp')

Class Msgp

new Msgp()

Create a writeable like stream parser.

const msgp = new Msgp()
msgp.on('data', function (data) {
  console.log(data.toString())
})
someSocketStream.pipe(msgp)

msgp.write(buffer)

msgp.write(Msgp.encode(JSON.stringify({name: 'msgp'})))

Event: 'data'

msgp.on('data', (buffer) => {
  console.log(JSON.parse(buffer.toString())) // {name: 'msgp'}
})

Event: 'error'

Event: 'drain'

Event: 'null'

Event: 'finish'

Class Method: Msgp.encode(string)

Class Method: Msgp.encode(buffer)

Encode string or buffer to buffer packet.

Msgp.encode(JSON.stringify({name: 'msgp'}))
// result: <Buffer 0f 7b 22 6e 61 6d 65 22 3a 22 6d 73 67 70 22 7d>

Class Method: Msgp.decode(buffer)

Decode buffer packet to message buffer.

let buf = new Buffer([0x0f, 0x7b, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x6d, 0x73, 0x67, 0x70, 0x22, 0x7d])
JSON.parse(Msgp.decode(buf))
// result: {name: 'msgp'}

License

MIT © zensh

Package Sidebar

Install

npm i msgp

Weekly Downloads

0

Version

1.0.0

License

none

Last publish

Collaborators

  • zensh