When subscribing to large Solana blocks over WebSocket, you may encounter a 1009
error or a PayloadTooBig
exception indicating that an incoming message exceeded the client’s maximum allowed size. This article explains:
- What WebSocket close code 1009 means
- How Python’s websockets library enforces a default limit
- How NodeJS ws library handles message-size limits
- Concrete fixes for both clients
1. What WebSocket close code 1009 means
Per the WebSocket RFC 6455, 1009 (MESSAGE_TOO_BIG
) signals that an endpoint received a frame or message larger than it is willing or able to process. In practice:
-
Python websockets lib throws a
PayloadTooBig
(then closes with code 1009). -
Node ws lib emits a
'close'
event withcode === 1009
.
This is a protection against resource exhaustion or Denial-of-Service attacks.
2. Python “websockets” Default Limits
By default, the websockets
library sets:
-
max_size=2**20
bytes (1 MiB) per message -
max_queue=32
messages buffered
Any incoming frame > 1 MiB triggers:
websockets.exceptions.PayloadTooBig: over size limit (length > max_size) ConnectionClosedError: sent 1009 (message too big)
3. Node.js “ws” Default Limits
The popular ws
library defaults to:
-
maxPayload=100 * 1024 * 1024
(100 MiB).
Although limit here is quite high, consider removing it completely.
4. Fixing the error
Python (asyncio websockets)
Increase or disable max_size
in your connect()
call:
import websockets async with websockets.connect( ws_url, max_size=None # disable limit, or set to a higher byte count ) as ws: …
-
max_size=None
⇒ no limit -
max_size=10*1024*1024
⇒ 10 MiB limit
Node.js (ws)
When instantiating the client, set maxPayload
:
const WebSocket = require('ws'); const ws = new WebSocket(WS_ENDPOINT, { maxPayload: Infinity // disable limit // or maxPayload: 10 * 1024 * 1024 // 10 MiB });
-
maxPayload: 0
orInfinity
⇒ no limit -
maxPayload: <bytes>
⇒ custom cap
If you still have questions or issues, feel free to reach out to our Support team by emailing support@chainstack.com or submitting this form.
Comments
0 comments
Article is closed for comments.