Unable to subscribe to more than 64 websocket market streams in Java

I’m trying to subscribe to 150 websocket market streams, but it always connects to exactly 64 streams. I tried Aggregate Trade Stream and Mark Price Stream and it always connects to 64 instead of 150.

I’m using the following lib:

log:

code:

package examples.um_futures.websocket;

import com.binance.connector.futures.client.impl.UMWebsocketClientImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public final class MarkPriceStreamTest {

    private final static List<String> symbols = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException, IOException {
        loadSymbols();
        UMWebsocketClientImpl client = new UMWebsocketClientImpl();

        for (String symbol : symbols) {
            Thread.sleep(300);
            subscribe(client, symbol);
        }
    }

    private static void subscribe(UMWebsocketClientImpl client, String symbol) {
        client.markPriceStream(symbol, 3, ((event) -> {
            System.out.println(symbol + " " + event);
        }));
    }

    public static void loadSymbols() throws IOException {
        InputStream inputStream = MarkPriceStreamTest.class.getResourceAsStream("/coins.txt");
        assert inputStream != null;
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
            while (reader.ready()) {
                String symbol = reader.readLine();
                symbols.add(symbol);
            }
        }
        System.out.println("symbols read from file: " + symbols.size());
    }
}

It’s not showing any errors in the logs. It simply does not connect after it reached 64 streams. Is there any limitation I might not aware of?
Thank you

Hi,

Why do you need to subscribe to so many streams? Why not just subscribe to the markPrice@arr (Mark Price Stream for All Market - returns mark price for all available symbols) and then filter the received data by whatever symbols you want data for?

Hi,
That’s a good point if I would only need the mark price, but when I want to use the Aggregate Trade Stream then the issue remains the same since it doesn’t have an endpoint for all markets.

I’ve solved the problem eventually. You need to use combineStreams then it will allow more than 64 streams.

example:

UMWebsocketClientImpl client = new UMWebsocketClientImpl(); 

ArrayList<String> streams = new ArrayList<>();
streams.add("btcusdt@aggTrade");
streams.add("bnbusdt@aggTrade");

client.combineStreams(streams, ((event) -> {
    System.out.println(event);
}));
1 Like

Thanks, I just encountered this problem