socketio/socket.io · error · Error
invalid format
Error message
invalid format
What it means
Thrown by the decode() helper in the basic-websocket-client example when an incoming Socket.IO packet fails validation in isPacketValid(). The decoder parses the type byte and JSON data, then checks the packet shape against the expected per-type contract (CONNECT must carry an object, DISCONNECT must have no data, EVENT must be a non-empty array with a string first element). A failure means the frame did not match any valid Socket.IO packet structure.
Source
Thrown at examples/basic-websocket-client/src/index.js:244
output += JSON.stringify(packet.data);
}
return output;
}
function decode(data) {
let i = 1; // skip "4" prefix
const packet = {
type: parseInt(data.charAt(i++), 10),
};
if (data.charAt(i)) {
packet.data = JSON.parse(data.substring(i));
}
if (!isPacketValid(packet)) {
throw new Error("invalid format");
}
return packet;
}
function isPacketValid(packet) {
switch (packet.type) {
case SIOPacketType.CONNECT:
return typeof packet.data === "object";
case SIOPacketType.DISCONNECT:
return packet.data === undefined;
case SIOPacketType.EVENT: {
const args = packet.data;
return (
Array.isArray(args) && args.length > 0 && typeof args[0] === "string"
);
}
default:View on GitHub (pinned to ae7fb46e08)
Solutions
- Inspect the raw frame being passed to decode() and confirm its type byte is one of 0 (CONNECT), 1 (DISCONNECT), or 2 (EVENT).
- If the server legitimately sends other packet types (ACK=3, ERROR=4, BINARY_EVENT=5), extend isPacketValid/SIOPacketType to model them.
- Wrap the decode() call in try/catch and skip/log frames that are not understood rather than crashing the client.
Example fix
// before
const packet = decode(raw);
handle(packet);
// after
let packet;
try {
packet = decode(raw);
} catch (e) {
console.warn('dropping malformed frame', raw, e.message);
return;
}
handle(packet); Defensive patterns
Strategy: try-catch
Validate before calling
function safeType(t){ return [0,1,2].includes(t); }
function looksValid(raw){
const t = parseInt(raw.charAt(1),10);
if(!safeType(t)) return false;
// rough shape checks matching isPacketValid
return true;
} Type guard
function isPacketShape(p){
switch(p.type){
case 0: return typeof p.data === 'object' && p.data !== null;
case 1: return p.data === undefined;
case 2: return Array.isArray(p.data) && p.data.length>0 && typeof p.data[0]==='string';
default: return false;
}
} Try / catch
let packet;
try { packet = decode(raw); }
catch (e) { console.warn('dropping frame', raw, e.message); return; }
handle(packet); Prevention
- Validate the type byte and JSON shape before trusting a decoded frame.
- Extend isPacketValid to model all packet types your client handles.
- Always wrap example/custom parser decode() in try/catch at the call site.
When it happens
Trigger: Calling decode() on a raw string that is malformed: a CONNECT (type 0) whose JSON body is not an object, a DISCONNECT (type 1) that carries trailing data, or an EVENT (type 2) whose payload is missing or whose first array element is not a string. Any type byte outside 0/1/2 also fails because isPacketValid returns false in the default branch.
Common situations: Manually decoding frames with a custom/naive parser in the example client, or pointing the example client at a server/endpoint that sends a non-standard or Engine.IO-only frame (e.g. an ERROR packet type 4 which this minimal client does not model).
Related errors
- unknown event name: ${eventName}
- Unknown type: ${obj}
- invalid payload
- illegal attachments
- got plaintext data when reconstructing a packet
AI-assisted analysis of socketio/socket.io@ae7fb46e08 (2026-08-03).
Data as JSON: /data/errors/dcebbadd3f8bcf40.json.
Report an issue: GitHub.