1009 api protocol_error ai_generated partial

WebSocket close code 1009: Message too large

ID: api/websocket-close-code-1009-message-too-large

Also available as: JSON · Markdown · 中文
78%Fix Rate
87%Confidence
1Evidence
2023-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
ws 8.16.0 (Node.js) active — — —
websockets 12.0 (Python) active — — —
Tornado 6.4.0 active — — —
Spring WebSocket 6.1.0 active — — —

Root Cause

The WebSocket frame or message size exceeds the server's configured maximum payload size, typically 64KB or 1MB, causing the connection to close.

generic

中文

WebSocket 帧或消息大小超出服务器配置的最大有效负载大小(通常为 64KB 或 1MB),导致连接关闭。

Official Documentation

https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#status_codes

Workarounds

  1. 90% success Increase the maximum message size on the server. For Node.js with 'ws': `const wss = new WebSocket.Server({ maxPayload: 1024 * 1024 });`. For Python with 'websockets': `async with websockets.serve(handler, host, port, max_size=2**20):`.
    Increase the maximum message size on the server. For Node.js with 'ws': `const wss = new WebSocket.Server({ maxPayload: 1024 * 1024 });`. For Python with 'websockets': `async with websockets.serve(handler, host, port, max_size=2**20):`.
  2. 85% success Compress large messages using a binary format like Protocol Buffers or MessagePack before sending over WebSocket. Example: serialize with `msgpack.pack(data)` and send as binary frame.
    Compress large messages using a binary format like Protocol Buffers or MessagePack before sending over WebSocket. Example: serialize with `msgpack.pack(data)` and send as binary frame.
  3. 75% success Implement message chunking on the client and reassembly on the server using a custom protocol (e.g., prefix each chunk with sequence number and total count).
    Implement message chunking on the client and reassembly on the server using a custom protocol (e.g., prefix each chunk with sequence number and total count).

中文步骤

  1. Increase the maximum message size on the server. For Node.js with 'ws': `const wss = new WebSocket.Server({ maxPayload: 1024 * 1024 });`. For Python with 'websockets': `async with websockets.serve(handler, host, port, max_size=2**20):`.
  2. Compress large messages using a binary format like Protocol Buffers or MessagePack before sending over WebSocket. Example: serialize with `msgpack.pack(data)` and send as binary frame.
  3. Implement message chunking on the client and reassembly on the server using a custom protocol (e.g., prefix each chunk with sequence number and total count).

Dead Ends

Common approaches that don't work:

  1. 70% fail

    The server may not support fragmentation or may reject partial messages if not properly reassembled.

  2. 90% fail

    TCP buffer size is unrelated to application-level WebSocket message limits.

  3. 60% fail

    The limit is typically server-configured, not library-specific; switching libraries rarely helps.