hello i am trying to i set up a webhook to binance for live update(ever sec) of future market prices
i keep getting error :++Rcv decoded: fin=1 opcode=1 data=b'{"id":1,"result":null}'
should be really easy but cant get it to work
or find a working example on google
any help will be welcome thx
code:
import websocket
import json
import threading
import time
def on_message(ws, message):
data = json.loads(message)
if 'data' in data:
# Extract the market price from the data
market_price = data['data']['markPrice']
print("Market Price:", market_price)
def on_error(ws, error):
print("Error:", error)
def on_close(ws, close_status_code, close_msg):
print("WebSocket closed")
def on_open(ws):
print("WebSocket connected")
# Subscribe to the BTCUSDT perpetual market price updates
subscription_message = {
"method": "SUBSCRIBE",
"params": ["btcusdt_perpetual@markPrice"],
"id": 1
}
ws.send(json.dumps(subscription_message))
def run_websocket():
websocket.enableTrace(True)
ws = websocket.WebSocketApp(
"wss://fstream.binance.com/ws/btcusdt_perpetual@markPrice",
on_message=on_message,
on_error=on_error,
on_close=on_close
)
ws.on_open = on_open
ws.run_forever()
if __name__ == "__main__":
# Run the WebSocket connection in a separate thread
websocket_thread = threading.Thread(target=run_websocket)
websocket_thread.start()
# Keep the main thread running to fetch prices every second
while True:
# Wait for 1 second
time.sleep(1)