socketio/socket.io · error · Error

unknown packet type ${p.type}

Error message

unknown packet type ${p.type}

What it means

Thrown by decodeString() when the first character of an encoded packet does not map to a known PacketType (CONNECT=0, DISCONNECT=1, EVENT=2, ACK=3, CONNECT_ERROR=4, BINARY_EVENT=5, BINARY_ACK=6). The leading byte must be a valid type id; anything else is an unknown packet type.

Source

Thrown at packages/socket.io-parser/lib/index.ts:228

      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);
    }

    // look up attachments if type binary
    if (
      p.type === PacketType.BINARY_EVENT ||
      p.type === PacketType.BINARY_ACK
    ) {
      const start = i + 1;
      while (str.charAt(++i) !== "-" && i != str.length) {}
      const buf = str.substring(start, i);
      if (buf != Number(buf) || str.charAt(i) !== "-") {
        throw new Error("Illegal attachments");
      }
      const n = Number(buf);
      if (!isInteger(n) || n < 1) {
        throw new Error("Illegal attachments");
      } else if (n > this.opts.maxAttachments) {
        throw new Error("too many attachments");

View on GitHub (pinned to ae7fb46e08)

Solutions

  1. Verify the encoder and decoder use the same Socket.IO protocol version so the leading type byte is consistent.
  2. Strip any Engine.IO wrapping/prefix correctly before handing the inner packet string to the Socket.IO decoder.
  3. Guard against empty/garbled frames at the transport layer and drop them before decoding.

Example fix

// before
decoder.add(raw); // raw has bad type byte -> 'unknown packet type 9'

// after
if (!/^[0-6]/.test(raw)) {
  return; // drop invalid frame
}
decoder.add(raw);
Defensive patterns

Strategy: validation

Validate before calling

const VALID_TYPES = new Set([0,1,2,3,4,5,6]);
function validTypeByte(s){ return VALID_TYPES.has(Number(s.charAt(0))); }

Type guard

function isKnownPacketType(t){ return [0,1,2,3,4,5,6].includes(Number(t)); }

Try / catch

try { decoder.add(raw); }
catch(e){ if(/unknown packet type/.test(e.message)){ return; } throw e; }

Prevention

When it happens

Trigger: Feeding the decoder a string whose first character is not 0-6, e.g. '9...', 'x...', or an empty/garbled frame where Number(str.charAt(0)) has no matching PacketType enum entry.

Common situations: A transport delivering truncated or corrupted frames; an Engine.IO version mismatch where the leading byte means something different; or a custom producer emitting raw JSON without the type prefix.

Related errors


AI-assisted analysis of socketio/socket.io@ae7fb46e08 (2026-08-03). Data as JSON: /data/errors/7596c6e483c692fb.json. Report an issue: GitHub.