Ir para o conteúdo principal
Latest blog post (July 25, 2024): npm package provenance.
Version: 4.x

Custom parser

Since Socket.IO v2.0.0, it is now possible to provide your own parser, in order to control the marshalling / unmarshalling of packets.

Server

import{Server}from"socket.io";

const io =newServer({
parser: myParser
});

Client

import{ io }from"socket.io-client";

const socket =io({
parser: myParser
});

Available parsers

Besides the default parser, here is the list of available parsers:

PackageDescription
socket.io-circular-parser Similar to the default parser, but handles circular references.
socket.io-msgpack-parser Uses MessagePack to encode the packets (based on the notepack.io package).
@skgdev/socket.io-msgpack-javascript Uses MessagePack to encode the packets (based on the @msgpack/msgpack package).
socket.io-json-parser Uses JSON.stringify() and JSON.parse() to encode the packets.
socket.io-cbor-x-parser Uses cbor-x to encode the packets.
@socket.io/devalue-parser Uses devalue to encode the packets.

Implementing your own parser

Here is a basic example with a parser that uses the JSON.stringify() and JSON.parse() methods:

import{Emitter}from"@socket.io/component-emitter";// polyfill of Node.js EventEmitter in the browser

classEncoder{
/**
* Encode a packet into a list of strings/buffers
*/
encode(packet){
return[JSON.stringify(packet)];
}
}

functionisObject(value){
returnObject.prototype.toString.call(value)==="[object Object]";
}

classDecoderextendsEmitter{
/**
* Receive a chunk (string or buffer) and optionally emit a "decoded" event with the reconstructed packet
*/
add(chunk){
const packet =JSON.parse(chunk);
if(this.isPacketValid(packet)){
this.emit("decoded", packet);
}else{
thrownewError("invalid format");
}
}
isPacketValid({ type, data, nsp, id }){
const isNamespaceValid =typeof nsp ==="string";
const isAckIdValid = id ===undefined||Number.isInteger(id);
if(!isNamespaceValid ||!isAckIdValid){
returnfalse;
}
switch(type){
case0:// CONNECT
return data ===undefined||isObject(data);
case1:// DISCONNECT
return data ===undefined;
case2:// EVENT
returnArray.isArray(data)&&typeof data[0]==="string";
case3:// ACK
returnArray.isArray(data);
case4:// CONNECT_ERROR
returnisObject(data);
default:
returnfalse;
}
}
/**
* Clean up internal buffers
*/
destroy(){}
}

exportconst parser ={Encoder,Decoder};

The default parser

The source code of the default parser (the socket.io-parser package) can be found here: https://github.com/socketio/socket.io-parser

Example of output:

  • basic emit
socket.emit("test",42);

will be encoded as:

2["test",42]
||
|└─ JSON-encoded payload
└─ packet type (2 => EVENT)
  • emit with binary, acknowledgement and custom namespace
socket.emit("test",Uint8Array.from([42]),()=>{
console.log("ack received");
});

will be encoded as:

51-/admin,13["test",{"_placeholder":true,"num":0}]
|||| || └─ JSON-encoded payload with placeholders for binary attachments
|||| |└─ acknowledgement id
|||| └─ separator
|||└─ namespace (not included when it's the main namespace)
||└─ separator
|└─ number of binary attachments
└─ packet type (5 => BINARY EVENT)

and an additional attachment (the extracted Uint8Array)

Pros:

Cons:

  • packets with binary content are sent as two distinct WebSocket frames (if the WebSocket connection is established)

The msgpack parser

This parser uses the MessagePack serialization format.

The source code of this parser can be found here: https://github.com/socketio/socket.io-msgpack-parser

Sample usage:

Server

import{Server}from"socket.io";
importcustomParserfrom"socket.io-msgpack-parser";

const io =newServer({
parser: customParser
});

Client (Node.js)

import{ io }from"socket.io-client";
importcustomParserfrom"socket.io-msgpack-parser";

const socket =io("https://example.com",{
parser: customParser
});

In the browser, there is now an official bundle which includes this parser:

In that case, you don't need to specify the parser option.

Pros:

  • packets with binary content are sent as one single WebSocket frame (if the WebSocket connection is established)
  • may result in smaller payloads (especially when using a lot of numbers)

Cons:

info

Please note that socket.io-msgpack-parser relies on the notepack.io MessagePack implementation. This implementation mainly focuses on performance and minimal bundle size, and thus does not support features like extension types. For a parser based on the official JavaScript implementation, please check this package.

AltStyle によって変換されたページ (->オリジナル) /