3.0.2 • Published 1 year ago

socket.io-msgpack-parser v3.0.2

Weekly downloads
486
License
MIT
Repository
github
Last release
1 year ago

socket.io-msgpack-parser

An alternative to the default socket.io-parser, encoding and decoding packets with msgpack.

With that parser, the browser build will be a bit heavier (an additional 7.5 KB minified, 3.0 KB gzipped), but each message will be smaller (sent as binary).

Please note that you MUST use the parser on both sides (server & client).

See also:

Compatibility table:

Parser versionSocket.IO server version
2.x1.x / 2.x
3.x3.x / 4.x

Usage

const io = require('socket.io');
const ioc = require('socket.io-client');
const customParser = require('socket.io-msgpack-parser');

const server = io(PORT, {
  parser: customParser
});

const socket = ioc('ws://localhost:' + PORT, {
  parser: customParser
});

socket.on('connect', () => {
  socket.emit('hello');
});

Format

socket.emit('hello', 'you') will create the following packet:

{
  "type": 2,
  "nsp": "/",
  "data": ["hello", "you"]
}

which will be encoded by the parser as:

<Buffer 83 a4 74 79 70 65 02 a3 6e 73 70 a1 2f a4 64 61 74 61 92 a5 68 65 6c 6c 6f a3 79 6f 75>

More information about the exchange protocol can be found here.