Socket
Socket
Sign inDemoInstall

socket.io-parser

Package Overview
Dependencies
3
Maintainers
2
Versions
53
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

socket.io-parser

socket.io protocol parser


Version published
Maintainers
2
Weekly downloads
7,162,186
decreased by-2.79%

Weekly downloads

Package description

What is socket.io-parser?

The socket.io-parser npm package is a component of the Socket.IO ecosystem, which is used for encoding and decoding messages that are sent over the Socket.IO protocol. It handles the serialization and deserialization of the various types of messages that can be transmitted, such as events, binary data, and control packets.

What are socket.io-parser's main functionalities?

Encoding messages

This feature allows you to encode messages into a format that can be transmitted over the Socket.IO protocol. The code sample represents a JSON string that has been encoded by the socket.io-parser for an event with some data.

{"type":2,"nsp":"/","data":["event",{"some":"data"}]}

Decoding messages

This feature is used to decode messages received over the Socket.IO protocol. The code sample shows how an encoded message (a string) is decoded back into its original form using the socket.io-parser.

const encodedMessage = '4hello world';
const decodedMessage = parser.decode(encodedMessage);
console.log(decodedMessage);

Binary support

The socket.io-parser can handle binary data by encoding and decoding it as part of the message. The code sample demonstrates how to encode an ArrayBuffer as binary data within a message.

const binaryData = new ArrayBuffer(100);
const message = parser.encodeAsBinary({ type: 5, data: binaryData });
console.log(message);

Other packages similar to socket.io-parser

Readme

Source

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

FAQs

Last updated on 27 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc