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

Commit

Permalink
added batchTickers option to handle single or batch
Browse files Browse the repository at this point in the history
  • Loading branch information
karimhossenbux committed May 22, 2021
1 parent 2f2f9bd commit 69c6039
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/exchanges/binance-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class BinanceBase extends BasicClient {
restThrottleMs = 1000,
l2updateSpeed = "",
l2snapshotSpeed = "",
batchTickers,
} = {}) {
super(wssPath, name, undefined, watcherMs);
this._restL2SnapshotPath = restL2SnapshotPath;
Expand All @@ -57,6 +58,7 @@ class BinanceBase extends BasicClient {
this.hasCandles = true;
this.hasLevel2Snapshots = true;
this.hasLevel2Updates = true;
this.batchTickers = batchTickers;

this._messageId = 0;
this._tickersActive = false;
Expand All @@ -83,10 +85,11 @@ class BinanceBase extends BasicClient {
_sendSubTicker(remote_id) {
if (this._tickersActive) return;
this._tickersActive = true;
const params = this.batchTickers ? ['!ticker@arr'] : [`${remote_id.toLowerCase()}@ticker`];
this._wss.send(
JSON.stringify({
method: "SUBSCRIBE",
params: [`${remote_id.toLowerCase()}@ticker`],
params: params,
id: ++this._messageId,
})
);
Expand All @@ -95,10 +98,11 @@ class BinanceBase extends BasicClient {
_sendUnsubTicker(remote_id) {
if (this._tickerSubs.size > 1) return;
this._tickersActive = false;
const params = this.batchTickers ? ['!ticker@arr'] : [`${remote_id.toLowerCase()}@ticker`];
this._wss.send(
JSON.stringify({
method: "UNSUBSCRIBE",
params: [`${remote_id}@ticker`],
params: params,
id: ++this._messageId,
})
);
Expand Down Expand Up @@ -204,7 +208,20 @@ class BinanceBase extends BasicClient {
return;
}

// ticker
// batch tickers
if (msg.stream === "!ticker@arr") {
for (let raw of msg.data) {
let remote_id = raw.s;
let market = this._tickerSubs.get(remote_id);
if (!market) continue;

let ticker = this._constructTicker(raw, market);
this.emit("ticker", ticker, market);
}
return;
}

// single ticker
if (msg.stream.toLowerCase().endsWith("ticker")) {
let remote_id = msg.data.s;
let market = this._tickerSubs.get(remote_id);
Expand Down
2 changes: 2 additions & 0 deletions src/exchanges/binance-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class BinanceClient extends BinanceBase {
watcherMs,
l2updateSpeed,
l2snapshotSpeed,
batchTickers = true,
} = {}) {
super({
name: "Binance",
Expand All @@ -23,6 +24,7 @@ class BinanceClient extends BinanceBase {
watcherMs,
l2updateSpeed,
l2snapshotSpeed,
batchTickers,
});
}
}
Expand Down

0 comments on commit 69c6039

Please sign in to comment.