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

strong-roots-capital/bitmex-heartbeat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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