future track order status java

Hello.
I am in the following situation:
I am currently using GitHub - binance/binance-futures-connector-java. I want to know how to track the progress of a trade, such as knowing when a limit order has been executed.
Currently, I am using Query Order to determine when an order has been executed. However, after 90-120 minutes, I receive a connection reset error. I want to mention that I execute Query Order every time I receive an event through klineStream, which may be why I am losing the connection after a large volume of queries.
From what I have read, there is another way to track the progress of a trade using Binance API Documentation. Although I have tried to use this method, I am not receiving any events.
I would like an example in Java to understand how it works. This is how I tried to use the second method but without any results:
websocketClient.listenUserStream(connectionService.getListenKey(), (event) → {
System.out.println("Event: " + event);
});
Currently, I am using wss://stream.binancefuture.com. I am still in the development phase, but I don’t think that could be the issue with listenUserStream.

Thank you in advance!

Hi @SilentUp94 - I tested it for myself using the Binance Futures Java Connector. Please refer to my attached screenshot. I created a listenKey on Futures Testnet and then connected to the User Stream and proceeded to make a couple of BTCUSDT orders on the Futures Testnet. All this activity appears in the websocket stream.

Here is the exact code I used to achieve this:

package um_futures;
import com.binance.connector.client.impl.UMFuturesClientImpl;
import com.binance.connector.client.impl.UMWebsocketClientImpl;
import config.PrivateConfig;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Getting listenkey and establing a user data stream.
 */

public class UserDataStream {
    private static final Logger logger = LoggerFactory.getLogger(UserDataStream.class);
    public static void main(String[] args) {
        UMWebsocketClientImpl wsClient = new UMWebsocketClientImpl("wss://stream.binancefuture.com");
        UMFuturesClientImpl futuresClient = new UMFuturesClientImpl(PrivateConfig.TESTNET_API_KEY, PrivateConfig.TESTNET_SECRET_KEY, PrivateConfig.BASE_URL);

        JSONObject obj = new JSONObject(futuresClient.userData().createListenKey());
        String listenKey = obj.getString("listenKey");
        logger.info("listenKey:" + listenKey);

        wsClient.listenUserStream(listenKey, ((event) -> {
            logger.info(event);
        }));
    }
}
1 Like

Hi, I found the problem. Because sometimes I am an idiot, I forgot that I need to extract the “listenkey” from the JSON, even if the JSON has only one parameter. Thank you very much for your help and I apologize for the time consumed. Now everything is working. You are the best!!

Hi,

As you’ve identified, you can use the UserData Websocket Stream because it definitely sounds like you’re calling Query Order far too often.

Also you mentioned you’re using wss://stream.binancefuture.com which is the Testnet Websocket URL. That’s fine but please make sure that you’re making the trades on the Testnet and that when generating the listenKey you made sure to set the BASE_URL to the Testnet URL as well (https://testnet.binancefuture.com)

You need a listenKey which you can generate with the below snippet.

UMFuturesClientImpl client = new UMFuturesClientImpl(TESTNET_API_KEY, TESTNET_SECRET_KEY, "https://testnet.binancefuture.com");
String listenKey = client.userData().createListenKey();

Then you feed the generated listenKey into the listenUserStream websocket method of which an example implementation is below:

UMWebsocketClientImpl client = new UMWebsocketClientImpl("wss://stream.binancefuture.com");
client.listenUserStream(listenKey, ((event) -> {
    System.out.println(event);
    client.closeAllConnections();
})); 

Examples from Java Futures Connector Repo:

I had already done everything as described above, but it still doesn’t work, I’m not receiving any events. I have tried different combinations and still not receiving any events. I tried opening a trade and then opening the stream, and vice versa. To me, it seems like there’s something wrong with listenUserStream. Can someone who is using this stream confirm that it works?