@strong-roots-capital/bitmex-heartbeat
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

bitmex-heartbeat Build status npm version codecov

Handles websocket ping/pong with BitMEX

Install

npm install @strong-roots-capital/bitmex-heartbeat

Use

import BitmexHeartbeat from '@strong-roots-capital/bitmex-heartbeat'
import BitMEXClient from 'bitmex-realtime-api'

const bitmexClient = new BitMEXClient()
const heartbeat = new BitmexHeartbeat(bitmexClient)
// Heartbeat begins on 'open' event of bitmexClient

// `heartbeat` emits `error` event if server stops responding, or if
// there are no listeners to the `error` event, an error is thrown instead
heartbeat.on('error', (error: Error) => {
    // try reconnecting
})

Important: the BitMEX server only seems to respond to plain-text pings, but it will respond to the ping as an error. Circumvent this by ignoring pong-related errors in your error handler

errorHandler(...error: string[]) {
    if (this.heartbeat.isServerResponse(error)) { return /* no problem here */ }
    console.log('Error:', ...error)
    process.exit(1)
}

Related

Documentation

Package Sidebar

Install

npm i @strong-roots-capital/bitmex-heartbeat

Weekly Downloads

4

Version

1.0.1

License

ISC

Unpacked Size

14.3 kB

Total Files

6

Last publish

Collaborators

  • amchelle
  • hamroctopus