Skip to content

Auto reconnect WebSocket

Nikolay Nozdrin-Plotnitsky edited this page Jun 13, 2018 · 1 revision
const BitShares = require("btsdex"),
      {Apis} = require("bitsharesjs-ws");

Apis.setRpcConnectionStatusCallback(statusCallBack)

function statusCallBack(status) {
  if (status === 'closed') {
    console.log("Status connection: closed")
    let reconnectTimer = setInterval(async () => {
      try {
        await BitShares.reconnect()
        clearInterval(reconnectTimer)
      } catch(error) {
        console.log(error)
      }
    }, 1000)
  }
}