socketio/socket.io · error · Error
Unknown type: ${obj}
Error message
Unknown type: ${obj} What it means
Thrown by the parser Decoder.add() when add() receives a value that is neither a string, a binary object (ArrayBuffer/Buffer/Blob), nor an object with base64. The else branch at the end of add() catches any input type the decoder does not know how to dispatch.
Source
Thrown at packages/socket.io-parser/lib/index.ts:210
this.reconstructor = new BinaryReconstructor(packet);
} else {
// non-binary full packet
super.emitReserved("decoded", packet);
}
} else if (isBinary(obj) || obj.base64) {
// raw binary data
if (!this.reconstructor) {
throw new Error("got binary data when not reconstructing a packet");
} else {
packet = this.reconstructor.takeBinaryData(obj);
if (packet) {
// received final buffer
this.reconstructor = null;
super.emitReserved("decoded", packet);
}
}
} else {
throw new Error("Unknown type: " + obj);
}
}
/**
* Decode a packet String (JSON data)
*
* @param {String} str
* @return {Object} packet
*/
private decodeString(str): Packet {
let i = 0;
// look up type
const p: any = {
type: Number(str.charAt(0)),
};
if (PacketType[p.type] === undefined) {
throw new Error("unknown packet type " + p.type);View on GitHub (pinned to ae7fb46e08)
Solutions
- Ensure only valid encoded inputs reach add(): an encoded packet string, an ArrayBuffer/Buffer/Blob, or a base64 object.
- Add an input-type guard at the transport boundary to coerce or reject non-frame values before calling add().
- If you control the producer, serialize the packet via the Encoder before sending.
Example fix
// before
decoder.add(42); // throws 'Unknown type: 42'
// after
if (typeof value === 'string' || isBinary(value)) {
decoder.add(value);
} else {
throw new TypeError('decoder.add expects a string or binary frame');
} Defensive patterns
Strategy: type-guard
Validate before calling
function isDecodable(obj){
return typeof obj==='string' || isBinary(obj) || (obj && typeof obj==='object' && obj.base64);
} Type guard
function isBinary(x){ return x instanceof ArrayBuffer || (typeof Buffer!=='undefined'&&Buffer.isBuffer(x)) || (typeof Blob!=='undefined'&&x instanceof Blob); } Try / catch
if(!isDecodable(value)) throw new TypeError('decoder.add expects a string or binary frame');
decoder.add(value); Prevention
- Validate input type at the transport boundary before calling add().
- Serialize via the Encoder rather than feeding raw values.
- Reject numbers/booleans/plain objects in test fixtures.
When it happens
Trigger: Calling decoder.add(value) where value is e.g. a number, boolean, plain object (non-base64), null, or undefined.
Common situations: A custom/buggy transport passing non-frame values to the decoder; or a unit test feeding literal numbers/objects instead of encoded packet strings.
Related errors
- invalid payload
- invalid format
- unknown event name: ${eventName}
- got plaintext data when reconstructing a packet
- got binary data when not reconstructing a packet
AI-assisted analysis of socketio/socket.io@ae7fb46e08 (2026-08-03).
Data as JSON: /data/errors/f80d4037eed9f7d9.json.
Report an issue: GitHub.