SUBSCRIBE - invalid state.

Hi,

I have set everything up with websockets and all is fine if I connect to;

./exe wss://fstream.binance.com/ws/btcusdt@ticker

My socket connection is esablished and handler called ok.

Received: {“e”:“24hrTicker”,“E”:1702742074270,“s”:“BTCUSDT”,“p”:“652.30”,“P”:“1.558”,“w”:“42149.75”,“c”:“42519.00”,“Q”:“0.006”,“o”:“41866.70”,“h”:“42577.00”,“l”:“41638.10”,“v”:“185779.441”,“q”:“7830556909.16”,“O”:1702655640000,“C”:1702742074269,“F”:4383430762,“L”:4385556923,“n”:2126134}

Thats fine.

However if I connect to;

wss://fstream.binance.com/ws (or indeed wss://fstream.binance.com/ws/ )

I connect OK but keep getting an “invalid state” back. The output below illustrates that my SUBSCRIBE message is fine (looks ok to me) and that before doing the send, I am successfully connected to websocket.

Successfully connected to: wss://fstream.binance.com/ws
sending: {
“method”: “SUBSCRIBE”,
“params”: [
{
“symbol”: “btcusdt@ticker”
}
],
“id”: 23 }
Error sending subscribe: invalid state
Running websocket: wss://fstream.binance.com/ws
[2023-12-16 15:56:57] [connect] Successful connection
[2023-12-16 15:56:58] [connect] WebSocket Connection 52.192.113.82:443 v-2 “WebSocket++/0.8.2” /ws 101

Can anybody shed light PLEASE :slight_smile:

“Connected” above means that
_connection = _tls_client.get_connection(_endpoint.c_str(), _ec);
_tls_client.connect(_connection);
_tls_client.run();

have all completed OK.

I am fairly sure the message I’m sending has to be the source of it but I’m stumped.

Thanks much for any help. Its wrecking my head!! :slightly_smiling_face:

Graham

aisling$ websocat -v 'wss://fstream.binance.com/ws'
[INFO  websocat::lints] Auto-inserting the line mode
[INFO  websocat::stdio_threaded_peer] get_stdio_peer (threaded)
[INFO  websocat::ws_client_peer] get_ws_client_peer
[INFO  websocat::ws_client_peer] Connected to ws
{"method":"SUBSCRIBE", "params":["btcusdt@ticker"], "id":1}   
{"result":null,"id":1}
{"e":"24hrTicker","E":1702872711302,"s":"BTCUSDT","p":"-900.40","P":"-2.145","w":"41726.92","c":"41085.50","Q":"0.005","o":"41985.90","h":"42476.00","l":"40810.60","v":"255310.826","q":"10653333929.74","O":1702786260000,"C":1702872711301,"F":4386565311,"L":4389438916,"n":2873587}

Hello, I’ve used the terminal to connect and subscribe to the WS using websocat and was successful.
Comparing the subscribe message, I think you’re unnecessary sending an extra "symbol" key element.

1 Like

Thanks a million Aisling. Sorted! Have a great day.

G