Skip to content
This repository was archived by the owner on Sep 9, 2023. It is now read-only.

Commit 29329d0

Browse files
authored
Merge pull request #280 from karimhossenbux/master
binance: use symbol@ticker instead of !ticker@arr
2 parents 2023e1e + 5879eb7 commit 29329d0

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/exchanges/binance-base.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class BinanceBase extends BasicClient {
4444
restThrottleMs = 1000,
4545
l2updateSpeed = "",
4646
l2snapshotSpeed = "",
47+
batchTickers = true,
4748
} = {}) {
4849
super(wssPath, name, undefined, watcherMs);
4950
this._restL2SnapshotPath = restL2SnapshotPath;
@@ -57,6 +58,7 @@ class BinanceBase extends BasicClient {
5758
this.hasCandles = true;
5859
this.hasLevel2Snapshots = true;
5960
this.hasLevel2Updates = true;
61+
this.batchTickers = batchTickers;
6062

6163
this._messageId = 0;
6264
this._tickersActive = false;
@@ -80,25 +82,27 @@ class BinanceBase extends BasicClient {
8082
super._onClosing();
8183
}
8284

83-
_sendSubTicker() {
85+
_sendSubTicker(remote_id) {
8486
if (this._tickersActive) return;
8587
this._tickersActive = true;
88+
const params = this.batchTickers ? ['!ticker@arr'] : [`${remote_id.toLowerCase()}@ticker`];
8689
this._wss.send(
8790
JSON.stringify({
8891
method: "SUBSCRIBE",
89-
params: ["!ticker@arr"],
92+
params: params,
9093
id: ++this._messageId,
9194
})
9295
);
9396
}
9497

95-
_sendUnsubTicker() {
98+
_sendUnsubTicker(remote_id) {
9699
if (this._tickerSubs.size > 1) return;
97100
this._tickersActive = false;
101+
const params = this.batchTickers ? ['!ticker@arr'] : [`${remote_id.toLowerCase()}@ticker`];
98102
this._wss.send(
99103
JSON.stringify({
100104
method: "UNSUBSCRIBE",
101-
params: ["!ticker@arr"],
105+
params: params,
102106
id: ++this._messageId,
103107
})
104108
);
@@ -204,7 +208,7 @@ class BinanceBase extends BasicClient {
204208
return;
205209
}
206210

207-
// ticker
211+
// batch tickers
208212
if (msg.stream === "!ticker@arr") {
209213
for (let raw of msg.data) {
210214
let remote_id = raw.s;
@@ -217,6 +221,17 @@ class BinanceBase extends BasicClient {
217221
return;
218222
}
219223

224+
// single ticker
225+
if (msg.stream.toLowerCase().endsWith("ticker")) {
226+
let remote_id = msg.data.s;
227+
let market = this._tickerSubs.get(remote_id);
228+
if (!market) return;
229+
230+
let ticker = this._constructTicker(msg.data, market);
231+
this.emit("ticker", ticker, market);
232+
return;
233+
}
234+
220235
// trades
221236
if (msg.stream.toLowerCase().endsWith("trade")) {
222237
let remote_id = msg.data.s;

src/exchanges/binance-client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class BinanceClient extends BinanceBase {
1313
watcherMs,
1414
l2updateSpeed,
1515
l2snapshotSpeed,
16+
batchTickers = true,
1617
} = {}) {
1718
if (testNet) {
1819
wssPath = "wss://testnet.binance.vision/stream";
@@ -30,6 +31,7 @@ class BinanceClient extends BinanceBase {
3031
watcherMs,
3132
l2updateSpeed,
3233
l2snapshotSpeed,
34+
batchTickers,
3335
});
3436
}
3537
}

0 commit comments

Comments
 (0)