Listening websocket not working properly

I am trying to listen to events happening on the account, I am using this code to do that

import websocket
from binance.um_futures import UMFutures

um_futures_client = UMFutures()
FUTURES_STREAM_END_POINT_1 = "wss://fstream.binance.com"
print(um_futures_client.time())

um_futures_client = UMFutures(key='XXXX',
                              secret='YYYY',
                              base_url='https://testnet.binancefuture.com')

listen_key = um_futures_client.new_listen_key()['listenKey']
print(listen_key)
url = f"{FUTURES_STREAM_END_POINT_1}/ws/{listen_key}"


def on_open(self):
    print(f"Open: futures order stream connected")


def on_message(self, ws):
    print(f"Message: {ws}")


def on_error(self, ws):
    print(f"Error: {ws}")


def on_close(self, close_status_code, close_msg):
    print(f"Close: {close_status_code} {close_msg}")


def stream_ticker():
    ws = websocket.WebSocketApp(url=url,
                                on_open=on_open,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    ws.run_forever(ping_interval=300)


stream_ticker()
input()

but when I manually open a trade on the account nothing happens, I don’t get any event printed in the console

Hi Bounty,

Your issue is that you’re using the prod Websocket but are using testnet to fetch your listen_key, hence they are mismatched. Additionally the code is wrong. Please follow the example in the README for Futures Python Connector: GitHub - binance/binance-futures-connector-python

Thanks,

So I checked the link and everything I ended up with this

import os, certifi, win32api
import time
from binance.um_futures import UMFutures
from binance.websocket.um_futures.websocket_client import UMFuturesWebsocketClient
os.environ['SSL_CERT_FILE'] = certifi.where()

um_futures_client = UMFutures()
FUTURES_STREAM_END_POINT_1 = "wss://fstream.binance.com"

print(um_futures_client.time())

um_futures_client = UMFutures(key='XXXX',
                              secret='YYYY',
                              base_url='https://testnet.binancefuture.com')

listen_key = um_futures_client.new_listen_key()['listenKey']
print(listen_key)


def message_handler(message):
    print(message)


print("WS Loading...")
ws_client = UMFuturesWebsocketClient()
ws_client.start()

ws_client.user_data(listen_key=listen_key, callback=message_handler, id=1)
print("WS Launched")
ws_client.join()

{'id': 1, 'result': None} this got printed in the console but I got no updates, am I doing it correctly?

You forgot to put in the websocket client part. So all your code is doing is fetching a listenKey, not subscribing to a stream.

Yes but like how to subscribe to user data stream? I am trying to get event when the user opens a new position. I am a bit confused, sorry if this is a silly question :sweat_smile:

Ahh I wasn’t able to scroll on your past comment so it looked like you had left out everything after print("WS Loading..."). Ignore my previous comment.

Your issue is that you’re using the production Websocket Stream URL whilst you’re using the Testnet REST API URL. Therefore there’s a mismatch as you’re trying to use a testnet listenKey on the prod Websocket Stream.

Please try the code below, it should help you.

import time
from binance.um_futures import UMFutures
from binance.websocket.um_futures.websocket_client import UMFuturesWebsocketClient


def message_handler(message):
    print(message)


api_key = ""
client = UMFutures(key=api_key, base_url="https://testnet.binancefuture.com")
response = client.new_listen_key()

print("Listen key :", response["listenKey"])

ws_client = UMFuturesWebsocketClient(stream_url="wss://stream.binancefuture.com")
ws_client.start()

ws_client.user_data(
    listen_key=response["listenKey"],
    id=1,
    callback=message_handler,
)

time.sleep(30)

print("closing ws connection")
ws_client.stop()

Same problem

Please take a look at the code example I posted above: Listening websocket not working properly - #7 by jonte

It should help resolve your issue if you’re facing the same problem as OP.

The concept here is first you have to create listen key and only then you can listen to user data stream.
You can check this out on