Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binance stream trade via websocket problem

I am getting no response from the Binance api when executing the following code, is there anything that I could have missed here? do I need to create an account in order to get stream data through the api?

import json
import websocket

socket='wss://stream.binance.com:9443'

def on_open(ws):
    print("opened")
    subscribe_message = {
        "method": "SUBSCRIBE",
        "params":
        [
         "btcusdt@trade",
         "btcusdt@depth"
         ],
         "id": 1
         }

    ws.send(json.dumps(subscribe_message))

def on_message(ws, message):
    print("received a message")
    print(json.loads(message))     

def on_close(ws):
    print("closed connection")        

ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message, on_close=on_close)
ws.run_forever()
like image 435
Shady Alkhouri Avatar asked Nov 05 '25 05:11

Shady Alkhouri


1 Answers

In order to make your code work you just need to add /ws at the end of the websocket url:

socket = 'wss://stream.binance.com:9443/ws'
like image 68
xilopaint Avatar answered Nov 06 '25 20:11

xilopaint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!