binance / binance-connector-typescript

Simple Typescript connector to Binance API
MIT License
64 stars 29 forks source link

Bad combinedStreams URL format when I using more two streams #23

Closed josbert-m closed 5 months ago

josbert-m commented 6 months ago

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(',', '/');
abanchev commented 6 months ago

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

josbert-m commented 6 months ago

@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('/')
]);
abanchev commented 6 months ago

@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('/') );
alplabin commented 5 months ago

This issue has been fixed in the latest release.