Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bitmex-simple-ws

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitmex-simple-ws - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

107

index.js

@@ -5,2 +5,4 @@ const WebSocket = require('ws');

const wait = n => new Promise(r => setTimeout(r, n));
class Connection extends EventEmitter {

@@ -13,2 +15,3 @@

this.connected = false;
this.reopen = true;

@@ -21,10 +24,3 @@ if(auth && auth.key && auth.secret) {

this.subscription = {};
this.book = {};
this.bookTopics = [
'orderBookL2_25',
'orderBookL2'
];
this.subscriptions = [];
}

@@ -47,3 +43,22 @@

reconnect = async () => {
if(!this.reopen) {
return;
}
this.connect();
await this.afterOpen;
await wait(200);
this.subscriptions.forEach(sub => {
sub.active = false;
this.subscribe(sub.name);
});
}
disconnect() {
this.reopen = false;
this.ws.disconnect();

@@ -70,29 +85,34 @@ }

readyHook();
console.log('opened!');
console.log('[BITMEX] opened!');
}
this.ws.onerror = e => console.error('error', e);
this.ws.onclose = e => console.log('onclose', e);
const onError = e => {
if(
e.message === 'Unexpected server response: 403' ||
e.message === 'Unexpected server response: 429'
) {
throw new Error(`[BITMEX] received "${.message}" need to back off reconnecting`)
}
this.ws.onmessage = this.handleMessage.bind(this);
console.log(new Date, '[BITMEX] error', e.message);
}
this.ws.on('error', onError)
this.ws.onerror = onError;
this.ws.onclose = async e => {
console.log(new Date, '[BITMEX] close');
this.emit('close');
this.connected = false;
await wait(1000);
this.reconnect();
}
this.ws.onmessage = this.handleMessage;
return this.afterOpen;
}
handleMessage(e) {
handleMessage = e => {
const payload = JSON.parse(e.data);
if(this.bookTopics.includes(payload.table)) {
const { symbol } = payload.data[0];
const id = `${payload.table}:${symbol}`;
if(this.book[id]) {
this.book[id].handle(payload);
this.emit(id, this.book[id]);
return;
}
}
this.emit('message', payload);

@@ -103,28 +123,25 @@ }

if(this.subscription[topic]) {
let registration;
this.subscriptions.forEach(sub => {
if(sub.name === topic) {
registration = sub;
}
});
if(registration && registration.active) {
console.log(new Date, '[BITMEX] refusing to subscribe to same topic twice', topic);
return;
}
if(!registration) {
this.subscriptions.push({name: topic, active: true});
} else if(!registration.active) {
registration.active = true;
}
this.ws.send(`{"op": "subscribe", "args": ["${topic}"]}`);
this.subscription[topic] = true;
}
// watchBook(symbol, topic = 'orderBookL2') {
// if(!this.bookTopics.includes(topic)) {
// throw new Error('This book topic is not supported');
// }
//
// const id = `${topic}:${symbol}`;
//
// if(this.book[id]) {
// return;
// }
//
// this.book[id] = new Book(id);
// this.rawSubscribe(id);
// }
}
module.exports = Connection;
{
"name": "bitmex-simple-ws",
"version": "1.0.2",
"version": "1.0.3",
"description": "",

@@ -25,5 +25,4 @@ "main": "index.js",

"dependencies": {
"lodash": "^4.17.11",
"ws": "^7.0.0"
}
}

@@ -7,3 +7,2 @@ # bitmex-simple-ws

- Handle reconnects
- Add heartbeat

@@ -10,0 +9,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc