๐Ÿ“ฆ socketio / socket.io-json-parser

socket.io parser based on JSON.stringify / JSON.parse

โ˜… 14 stars โ‘‚ 7 forks ๐Ÿ‘ 14 watching โš–๏ธ MIT License
jsonsocket-iosocket-io-parser
๐Ÿ“ฅ Clone https://github.com/socketio/socket.io-json-parser.git
HTTPS git clone https://github.com/socketio/socket.io-json-parser.git
SSH git clone git@github.com:socketio/socket.io-json-parser.git
CLI gh repo clone socketio/socket.io-json-parser
dependabot[bot] dependabot[bot] chore(deps): bump path-parse from 1.0.6 to 1.0.7 (#7) 3f5bb28 4 years ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“ test
๐Ÿ“„ .babelrc
๐Ÿ“„ .gitignore
๐Ÿ“„ .travis.yml
๐Ÿ“„ HISTORY.md
๐Ÿ“„ index.js
๐Ÿ“„ LICENSE
๐Ÿ“„ package-lock.json
๐Ÿ“„ package.json
๐Ÿ“„ README.md
๐Ÿ“„ README.md

socket.io-json-parser

An alternative to the default socket.io-parser, encoding and decoding packets with JSON.parse / stringify.

With that parser, binary data (ArrayBuffer / Buffer / Blob / File) is not supported.

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

See also:

  • the default parser: https://github.com/socketio/socket.io-parser
  • a parser based on msgpack: https://github.com/darrachequesne/socket.io-msgpack-parser

Usage

const io = require('socket.io');
const ioc = require('socket.io-client');
const customParser = require('socket.io-json-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:

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

More information about the exchange protocol can be found here.