๐Ÿ“ฆ socketio / socket.io-parser

โ˜… 140 stars โ‘‚ 106 forks ๐Ÿ‘ 140 watching
๐Ÿ“ฅ Clone https://github.com/socketio/socket.io-parser.git
HTTPS git clone https://github.com/socketio/socket.io-parser.git
SSH git clone git@github.com:socketio/socket.io-parser.git
CLI gh repo clone socketio/socket.io-parser
Dirk Stolle Dirk Stolle ci: update actions in GitHub Actions workflows (#117) a9758da 3 years ago ๐Ÿ“ History
๐Ÿ“‚ a9758da4be988419a1c17ddd2299833993712d5f View all commits โ†’
๐Ÿ“ .github
๐Ÿ“ bench
๐Ÿ“ lib
๐Ÿ“ support
๐Ÿ“ test
๐Ÿ“„ .gitignore
๐Ÿ“„ CHANGELOG.md
๐Ÿ“„ LICENSE
๐Ÿ“„ package-lock.json
๐Ÿ“„ package.json
๐Ÿ“„ postcompile.sh
๐Ÿ“„ Readme.md
๐Ÿ“„ tsconfig.esm.json
๐Ÿ“„ tsconfig.json
๐Ÿ“„ zuul.config.js
๐Ÿ“„ README.md

socket.io-parser

Build Status NPM version

A socket.io encoder and decoder written in JavaScript complying with version 5 of socket.io-protocol. Used by socket.io and socket.io-client.

Compatibility table:

Parser versionSocket.IO server versionProtocol revision
3.x1.x / 2.x4
4.x3.x5

Parser API

socket.io-parser is the reference implementation of socket.io-protocol. Read the full API here: socket.io-protocol.

Example Usage

Encoding and decoding a packet

var parser = require('socket.io-parser');
var encoder = new parser.Encoder();
var packet = {
  type: parser.EVENT,
  data: 'test-packet',
  id: 13
};
encoder.encode(packet, function(encodedPackets) {
  var decoder = new parser.Decoder();
  decoder.on('decoded', function(decodedPacket) {
    // decodedPacket.type == parser.EVENT
    // decodedPacket.data == 'test-packet'
    // decodedPacket.id == 13
  });

  for (var i = 0; i < encodedPackets.length; i++) {
    decoder.add(encodedPackets[i]);
  }
});

Encoding and decoding a packet with binary data

var parser = require('socket.io-parser');
var encoder = new parser.Encoder();
var packet = {
  type: parser.BINARY_EVENT,
  data: {i: new Buffer(1234), j: new Blob([new ArrayBuffer(2)])},
  id: 15
};
encoder.encode(packet, function(encodedPackets) {
  var decoder = new parser.Decoder();
  decoder.on('decoded', function(decodedPacket) {
    // decodedPacket.type == parser.BINARY_EVENT
    // Buffer.isBuffer(decodedPacket.data.i) == true
    // Buffer.isBuffer(decodedPacket.data.j) == true
    // decodedPacket.id == 15
  });

  for (var i = 0; i < encodedPackets.length; i++) {
    decoder.add(encodedPackets[i]);
  }
});
See the test suite for more examples of how socket.io-parser is used.

License

MIT