binance-connector-typescript icon indicating copy to clipboard operation
binance-connector-typescript copied to clipboard

Bad combinedStreams URL format when I using more two streams

Open josbert-m opened this issue 9 months ago • 3 comments

I'm encountering an error with the Binance Websocket Stream library

I'm working with code that utilizes the @binance/connector-typescript library to establish a Websocket connection to the Binance exchange and subscribe to kline (candlestick) data streams for specific symbols (btcusdt, bnbusdt, xrpusdt). However, I'm encountering an error when subscribing to multiple streams.

Code Snippet:

import { Interval, WebsocketStream } from '@binance/connector-typescript';

const symbols = ['btcusdt', 'bnbusdt', 'xrpusdt'];
const wsURL = 'wss://stream.binance.com:443';
const callbacks = {};

const streamClient = new WebsocketStream({
    callbacks,
    wsURL,
    combinedStreams: true,
});

streamClient.subscribe(
    symbols.map((s) => `${s}@kline_${Interval['1m']}`)
);

Error Message:

[2024-05-05T02:32:29.625Z] [INFO] Sending Websocket connection to: wss://stream.binance.com:443/stream?streams=xrpusdt@kline_1m/btcusdt@kline_1m,bnbusdt@kline_1m
[2024-05-05T02:32:30.601Z] [ERROR] Received error from server
[2024-05-05T02:32:30.603Z] [ERROR] Error: Unexpected server response: 400

Expected Behavior:

The code should construct a URL for the Websocket subscription in the following format (without commas separating streams):

wss://stream.binance.com:443/stream?streams=xrpusdt@kline_1m/btcusdt@kline_1m/bnbusdt@kline_1m

Problem:

The current implementation generates a URL with commas between the streams, which appears to be causing the server to return an error (status code 400, indicating a bad request).

Possible Causes:

I think the problem is in the following line:

https://github.com/binance/binance-connector-typescript/blob/e2edefaf9a54fcf87529c840633b5b4d1b8d7226/src/websocketStream.ts#L23

Maybe it should be:

if (Array.isArray(stream)) stream = stream.toString().replaceAll(',', '/');

josbert-m avatar May 05 '24 02:05 josbert-m

As a temporary fix join them yourself and pass one string to subscribe

abanchev avatar May 05 '24 10:05 abanchev

@abanchev It's done, I did it as follows:

streamClient.subscribe([
    `${symbols[0]}@kline_${Interval['1m']}`,
    symbols.slice(1).map((s) => `${s}@kline_${Interval['1m']}`).join('/')
]);

josbert-m avatar May 05 '24 18:05 josbert-m

@abanchev It's done, I did it as follows:

streamClient.subscribe([
    `${symbols[0]}@kline_${Interval['1m']}`,
    symbols.slice(1).map((s) => `${s}@kline_${Interval['1m']}`).join('/')
]);

You can just do

streamClient.subscribe( symbols.map((s) => `${s}@kline_${Interval['1m']}`).join('/') );

abanchev avatar May 05 '24 19:05 abanchev