Hello all,
So I am trying a very simple script to authenticate and subscribe to a single level2 channel but I am getting the following error:
Connection closed with error: sent 1009 (message too big); no close frame received.
Here is my code:
import asyncio
import websockets
import json
import hmac
import hashlib
import time
API_KEY = ‘’
API_SECRET = ‘’
CHANNEL = ‘level2’
PRODUCT_ID = ‘ETH-USD’
WS_API_URL = ‘wss://advanced-trade-ws.coinbase.com’
def generate_signature(api_secret, channel, product_id):
timestamp = str(int(time.time()))
message = f"{timestamp}{channel}{product_id}" # Concatenating without commas
signature = hmac.new(api_secret.encode(), message.encode(), hashlib.sha256).hexdigest()
return timestamp, signature
async def subscribe(websocket):
timestamp, signature = generate_signature(API_SECRET, CHANNEL, PRODUCT_ID)
subscribe_message = {
“type”: “subscribe”,
“product_ids”: [PRODUCT_ID],
“channel”: CHANNEL,
“api_key”: API_KEY,
“signature”: signature,
“timestamp”: timestamp
}
await websocket.send(json.dumps(subscribe_message))
async def connect():
async with websockets.connect(WS_API_URL) as websocket:
await subscribe(websocket)
while True:
try:
message = await websocket.recv()
print(“Received message:”, message)
except websockets.exceptions.ConnectionClosedError as e:
print(f"Connection closed with error: {e}“)
break
except Exception as e:
print(f"An error occurred: {e}”)
if name == ‘main’:
asyncio.get_event_loop().run_until_complete(connect())
If anyone is able to give me some insight into what I am doing wrong I would greatly appreciate it.
Can you post how your generated JSON message looks? Remove your API key from it before posting.
EDIT: Check if your client allows to increase/change max message size.
Here is the JSON message that is being sent:
{“type”: “subscribe”, “product_ids”: [“ETH-USD”], “channel”: “level2”, “api_key”: “”, “signature”: “dceb4d7ea470863aad0be4b3452b906ac8c0f953da0bdf959954f120da84ad10”, “timestamp”: “1701516054”}
Thank you for trying to help with this.
Are you using this websocket?
From:
https://websockets.readthedocs.io/en/stable/reference/asyncio/common.html#websockets.legacy.protocol.WebSocketCommonProtocol
The max_size parameter enforces the maximum size for incoming messages in bytes. The default value is 1 MiB. If a larger message is received, recv() will raise ConnectionClosedError and the connection will be closed with code 1009.
You need to increase max_size
. Right now ETH-USD
snapshot message is 1.11618500 mb…
I believe I am using that package, at least the version matches the one from the link you posted. I have given that documentation a look over and it seems like there is a way to increase the max_size parameter. Unfortunately I am very new to coding so I am not sure how to implement it. Is this something you might be able to help me with?
Thanks for the replies regardless, very much appreciated.
1 Like
Sorry can not help more! I don’t use that library and also I don’t use Python. But I do believe your problem is size limit. You need to find a way to increase it.
1 Like
Thanks for the insight, and your time!
1 Like
I was able to figure out how to change the maxsize parameter, it worked! I am now able to successfully subscribe to the orderbook updates. Thanks again for your assistance!
1 Like