New connection with webSocket

I’m trying to connect to the user data streams of futures, I’m receiving the listenKey correctly, I create a new connection with webSocket to receive the events, when I start the program I receive that the connection with websocket was opened but soon after opening it is closed and the return of closeEvent it is an error 1006, could someone help me, follow the code below, thanks. Learning node plis help.

Code snippet written in typescript.


const userDataStream = new WebSocket (`wss://fstream-auth.binance.com/ws/${listenKey}?listenKey=${listenKey}`);

userDataStream.onopen = () => {

  console.log('WebSocket connection opened.');

};

userDataStream.onerror = (error) => {

  console.error('WebSocket connection error:', error);

};

userDataStream.onmessage = (event) => {

  console.log('Message received:', event.data);

};

userDataStream.onclose = (event) => {

  console.log('Connection WebSocket close:', event);

};

Hello, have you been handling the connection properly according to the API documentation, in particular the hand shake (ping pong) ?

You could also refer to this another post for some ideas: Websocket closes unexpectedly with error 1006 - #9 by tony.b

1 Like

As I couldn’t use User Date Stream, I connected with the basic endpoint and made a GET call, my return was a 404 error, I went after errors and couldn’t find them, I looked for questions here in the community and found a possible answer, I’m in a country where derivatives are restricted, could this be the problem with these errors? I’m in Brazil, it’s not allowed to trade futures here. thanks for helping me.

Besides the error code “404”, it also comes with an error message which provides more information about the error, you need to get that to understand the context. Otherwise, it’s not enough information.

As it seems you’re starting out with REST API, I’ll recommend to use GitHub - binance/binance-api-postman: Postman collection for Binance Public API, including spot, margin, futures, etc. first to get a start on how REST API works and be able to see the raw response, without programming code.

1 Like

Thanks for your help, I performed several tests in postman and with that I discovered that the error was not in the keys or url, but the version of axios that I was using, when I installed axios, apparently it installed version 1.0.0, when i checked this i installed axios@latest and it updated to the latest version and the error is gone thanks for your help

1 Like

Hi @Ruan_Souza_de_Lima - if you’re using typescript you might find it easier to use an SDK for your connectivity challenges. Websockets can be a bit fussy, especially since you should ideally also have safety mechanisms in place to handle issues (heartbeats, automatic reconnects & resubscribes, listen key keep alive & refresh workflows, etc). Here’s one that handles all this for you and will hopefully save you from a lot of pain: binance/ws-userdata.ts at master · tiagosiebler/binance · GitHub

Hope it helps

1 Like

Hello, thanks for your answer, my problem was a simple lack of attention and I got the solution just by putting the axios version in its most updated version, I understand that the skd would be easier, but I wanted to make a robot from 0, to test my programming and improve my learning.

@Ruan_Souza_de_Lima makes sense! I’m the same. If you want to try out the ping/pong behaviour yourself, add listeners for onping and onping (the same way you have onopen etc).

If the websocket sends you a pong, you should reply with a pong: ws.pong(). You will also need to implement:

  • keep alive cycles for the listen key (expires every hour by default unless you call the keep alive endpoint)
  • handlers if the ws dies and/or the listen key expires

If you’d like to see how I’m handling this in production environments, you can follow some of the logic from here:

Or if you’d like to follow the workflow from connecting a user data stream, it starts here:

Hope this helps

1 Like