From ee9e34a2fbedc0b5567c99e03aab8c544deefbe8 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Wed, 11 Nov 2020 21:31:15 +0000 Subject: [PATCH 1/2] fix(#21): add pass-thru options and URL override for WebSocket client. Update docs. --- README.md | 30 +++++++++++++++++++++++++++++- lib/websocket-client.js | 14 ++++++++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ca282a8..5099423 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,35 @@ const {WebsocketClient} = require('bybit-api'); const API_KEY = 'xxx'; const PRIVATE_KEY = 'yyy'; -const ws = new WebsocketClient({key: API_KEY, secret: PRIVATE_KEY}); +const wsConfig = { + key: API_KEY, + secret: PRIVATE_KEY, + + // The following parameters are optional: + + // defaults to false == testnet. set to true for livenet. + // livenet: true + + // override which URL to use for websocket connections + // wsUrl: 'wss://stream.bytick.com/realtime' + + // how often to check (in ms) that WS connection is still alive + // pingInterval: 10000, + + // how long to wait (in ms) before deciding the connection should be terminated & reconnected + // pongTimeout: 1000, + + // how long to wait before attempting to reconnect (in ms) after connection is closed + // reconnectTimeout: 500, + + // config options sent to RestClient (used for time sync). See RestClient docs. + // restOptions: { }, + + // config for axios to pass to RestClient. E.g for proxy support + // requestOptions: { } +}; + +const ws = new WebsocketClient(wsConfig); ws.subscribe(['position', 'execution', 'trade']); ws.subscribe('kline.BTCUSD.1m'); diff --git a/lib/websocket-client.js b/lib/websocket-client.js index 51e64cc..f8e9c55 100644 --- a/lib/websocket-client.js +++ b/lib/websocket-client.js @@ -1,5 +1,4 @@ -const {EventEmitter} = require('events'); - +const { EventEmitter } = require('events'); const WebSocket = require('ws'); const defaultLogger = require('./logger'); @@ -35,7 +34,7 @@ module.exports = class WebsocketClient extends EventEmitter { ...options }; - this.client = new RestClient(null, null, this.options.livenet); + this.client = new RestClient(null, null, this.options.livenet, this.options.restOptions, this.options.requestOptions); this._subscriptions = new Set(); this._connect(); @@ -65,12 +64,19 @@ module.exports = class WebsocketClient extends EventEmitter { this.ws && this.ws.close(); } + _getWsUrl() { + if (this.options.wsUrl) { + return this.options.wsUrl; + } + return wsUrls[this.options.livenet ? 'livenet' : 'testnet']; + } + async _connect() { try { if (this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING; const authParams = await this._authenticate(); - const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + authParams; + const url = this._getWsUrl() + authParams; this.ws = new WebSocket(url); From 049d9e8df21438bc18aeaa59d052019b3b8ade29 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Wed, 11 Nov 2020 21:32:48 +0000 Subject: [PATCH 2/2] v1.2.3: add pass-thru config & wsUrl param to WebsocketClient --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e3d6027..c459667 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bybit-api", - "version": "1.2.2", + "version": "1.2.3", "description": "A production-ready Node.js connector for the Bybit APIs and WebSockets", "main": "index.js", "scripts": {