Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Chrome closes the WebSocket with Invalid frame header

Your error

WebSocket connection to 'ws://localhost:8080/' failed: Invalid frame header1 more line it can show

Fix it fast

Most common cause, 1 of 3

The server uses a longer length form than the payload needs

Pick the form from the payload size, every time.

Fix
function frameHeader(length, opcode = 0x1) {
  const first = 0x80 | opcode; // FIN bit + opcode (0x1 text, 0x2 binary)
  if (length <= 125) return Buffer.from([first, length]);
  if (length <= 0xffff) {
    const h = Buffer.alloc(4);
    h[0] = first;
    h[1] = 126;
    h.writeUInt16BE(length, 2);
    return h;
  }
  const h = Buffer.alloc(10);
  h[0] = first;
  h[1] = 127;
  h.writeBigUInt64BE(BigInt(length), 2);
  return h;
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

"WebSocket connection to '...' failed: Invalid frame header" means Chrome or Edge could not parse a frame the server sent. In Chromium the cause is the payload length field: a longer form than needed, length bytes in the wrong order, or a frame over 2 GiB. Fix the server's encoder to use the shortest big-endian form from RFC 6455.

  • Applies to Chrome and Edge (Chromium), RFC 6455, Node.js 24 (example encoder)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Log the header bytes your server writes before each frame, next to the payload size, then send one short message (under 126 bytes), one medium (a few KB) and one large (over 64 KB). The size at which Chrome fails, and the second header byte, show which cause applies.

Run this first
// just before socket.write(header) in the server
console.log(payload.length, header.toString("hex"));

Then find the line you see in this list and open its fix:

  1. 1The server uses a longer length form than the payload needsThe second header byte is 7e (126) for a payload of 125 bytes or less, or 7f (127) for a payload of 65,535 bytes or less
  2. 2The extended length is written in the wrong byte orderMessages over 65,535 bytes fail at once, mid-sized ones arrive garbled or break the next frame, and the logged length bytes are reversed (2c01 instead of 012c for 300 bytes)
  3. 3A single frame is larger than Chromium acceptsOnly a huge transfer fails, with a correct header, for example a file sent as one message

What does the error look like?

  • WebSocket connection to 'ws://localhost:8080/' failed: Invalid frame header
  • The socket opens with 101, then fails on the first message above a certain size

Cause 1 of 3

The server uses a longer length form than the payload needs

You seeThe second header byte is 7e (126) for a payload of 125 bytes or less, or 7f (127) for a payload of 65,535 bytes or less

Check

Invalid frame header from a custom server most often means a non-minimal payload length. RFC 6455 allows three forms: the length itself in 7 bits up to 125, the marker 126 plus 16 bits up to 65,535, and the marker 127 plus 64 bits beyond that, and it requires the shortest one. Chromium's frame parser rejects a 126 form that carries 125 or less, and a 127 form that carries 65,535 or less.

Check
// second byte of a server frame (no mask bit): 0x00-0x7d = length, 0x7e = 16-bit, 0x7f = 64-bit
const marker = header[1] & 0x7f;
console.log({ marker, length: payload.length });

Fix

Pick the form from the payload size, every time.

Fix
function frameHeader(length, opcode = 0x1) {
  const first = 0x80 | opcode; // FIN bit + opcode (0x1 text, 0x2 binary)
  if (length <= 125) return Buffer.from([first, length]);
  if (length <= 0xffff) {
    const h = Buffer.alloc(4);
    h[0] = first;
    h[1] = 126;
    h.writeUInt16BE(length, 2);
    return h;
  }
  const h = Buffer.alloc(10);
  h[0] = first;
  h[1] = 127;
  h.writeBigUInt64BE(BigInt(length), 2);
  return h;
}

Verify

Send the three test messages again. All three arrive in the page's onmessage handler and the console shows no error.

Verify
ws.onmessage = (e) => console.log("got", e.data.length);

Confirmed by chromium.googlesource.com

Did this fix it?

Cause 2 of 3

The extended length is written in the wrong byte order

You seeMessages over 65,535 bytes fail at once, mid-sized ones arrive garbled or break the next frame, and the logged length bytes are reversed (2c01 instead of 012c for 300 bytes)

Check

Invalid frame header on messages above 65,535 bytes, while short ones work, points at the byte order of the extended length. RFC 6455 writes multibyte lengths in network byte order, which is big-endian, and the top bit of the 64-bit form must be 0. A little-endian write puts the low byte first, so the browser reads a length far above what Chromium accepts. A reversed 16-bit length does not always fail at once: it shifts the stream, and a later frame fails instead.

Check
// for a 300-byte payload the bytes after 7e must be 01 2c
console.log(header.subarray(2).toString("hex"));

Fix

Write the 16-bit and 64-bit lengths with explicit big-endian calls, as in the encoder above, instead of a platform-order conversion.

Fix
h.writeUInt16BE(length, 2);           // not writeUInt16LE
h.writeBigUInt64BE(BigInt(length), 2); // not writeBigUInt64LE

Verify

A 300-byte message logs 7e012c as its header tail and reaches the browser intact.

Verify
console.log(frameHeader(300).toString("hex")); // 817e012c

Confirmed by rfc-editor.org

Did this fix it?

Cause 3 of 3

A single frame is larger than Chromium accepts

You seeOnly a huge transfer fails, with a correct header, for example a file sent as one message

Check

Invalid frame header for a correctly encoded but enormous frame means the length passes 2^31 - 1 bytes, the largest frame Chromium's parser accepts; the channel reports that too-big case under the same message.

Check
console.log(payload.length > 2 ** 31 - 1);

Fix

Send large data in pieces: several smaller messages, each with its own sequence number, reassembled in the page.

Fix
const CHUNK = 1024 * 1024;
for (let i = 0; i < data.length; i += CHUNK) {
  sendFrame(data.subarray(i, i + CHUNK), 0x2);
}

Verify

The transfer completes and the page reassembles the same number of bytes that were sent.

Verify
console.log(received.reduce((n, c) => n + c.byteLength, 0));

Confirmed by chromium.googlesource.com

Did this fix it?

How do I stop it coming back?

  • Use a maintained WebSocket library for the server unless writing the protocol is the point of the project.
  • If you keep a custom encoder, unit-test it at the boundary sizes 125, 126, 65,535 and 65,536 bytes.
  • Write lengths only through explicit big-endian buffer calls.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.