Custom parser
Server
Client
Here is a basic example with a parser that uses the JSON.stringify()
and JSON.parse()
methods:
const Emitter = require("component-emitter"); // polyfill of Node.js EventEmitter in the browser
class Encoder {
/**
* Encode a packet into a list of strings/buffers
*/
encode(packet) {
return [JSON.stringify(packet)];
}
}
class Decoder extends Emitter {
/**
* 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 {
throw new Error("invalid format");
}
}
isPacketValid({ type, data, nsp, id }) {
const isNamespaceValid = typeof nsp === "string";
const isAckIdValid = id === undefined || Number.isInteger(id);
if (!isNamespaceValid || !isAckIdValid) {
return false;
}
switch (type) {
case 0: // CONNECT
return data === undefined || typeof data === "object";
case 1: // DISCONNECT
return data === undefined;
case 2: // EVENT
return Array.isArray(data) && data.length > 0;
case 3: // ACK
return Array.isArray(data);
case 4: // CONNECT_ERROR
return typeof data === "object";
default:
return false;
}
}
/**
* Clean up internal buffers
*/
destroy() {}
}
module.exports = { Encoder, Decoder };
The source code of the default parser (the package) can be found here: https://github.com/socketio/socket.io-parser
Example of output:
- basic emit
will be encoded as:
- emit with binary, acknowledgement and custom namespace
socket.emit("test", Uint8Array.from([42]), () => {
console.log("ack received");
});
Pros:
- the binary attachments is then base64-encoded, so this parser is compatible with browsers that , like IE9
Cons:
- packets with binary content are sent as two distinct WebSocket frames (if the WebSocket connection is established)
This parser uses the MessagePack serialization format.
The source code of this parser can be found here:
Sample usage:
Server
const httpServer = require("http").createServer();
const io = require("socket.io")(httpServer, {
parser: require("socket.io-msgpack-parser")
});
In the browser, there is now an official bundle which includes this parser:
- cdnjs: https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.1.3/socket.io.msgpack.min.js
- jsDelivr:
- unpkg: https://unpkg.com/socket.io-client@3.1.3/dist/socket.io.msgpack.min.js
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 results in smaller payloads (especially when using a lot of numbers)
Cons:
- incompatible with browsers that , like IE9
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 , please check this package.
Caught a mistake? Edit this page on