aloysius-pgast / bittrex-signalr-client

Node.js implementation of SignalR protocol tailored for Bittrex exchange
39 stars 14 forks source link

Please, what about if client.disconnect() does not happen. Question. #20

Closed hrivera2014 closed 5 years ago

hrivera2014 commented 5 years ago

I has rewrited the code of one of your examples in order to connect to all BTC pairs what about if clien.disconnect does not happen. for intance by a ctrl-c.

"use strict";
var market = []
var request = require('request')
const util = require('util');
const SignalRClient = require('bittrex-signalr-client');
let client = new SignalRClient({
    // websocket will be automatically reconnected if server does not respond to ping after 10s
    pingTimeout:10000,
    // use cloud scraper to bypass Cloud Fare (default)
    useCloudScraper:true
})

request("https://api.bittrex.com/api/v1.1/public/getmarketsummaries", function(err,responnse,body) {

    var data = JSON.parse(body)

    for(var index=0; index < data.result.length; index++){
      if(data.result[index].MarketName.substr(0, 3) == 'BTC') market.push(data.result[index].MarketName)
    }

    client.on('ticker', function(data){
        //console.log(util.format("Got ticker update for pair '%s'", data.pair));
        console.log(data);
    });

    });
    //-- start subscription

    client.subscribeToTickers(market);

    // disconnect client after 60s
    setTimeout(function(){
        console.log('=== Disconnecting...');
        client.disconnect();
        process.exit(0);
    }, 120000);
  }
)
aloysius-pgast commented 5 years ago

With a ctrl-c, SIGINT handler will be called. If no custom SIGINT handler was defined, process will exit

hrivera2014 commented 5 years ago

Thanks so much for reply to my, may be, obvious question. Ok I can handle the ctrl-c with a code like

process.on('SIGINT', function() {
  console.log("Caught interrupt signal")
  client.disconnect();
  process.exit(0)
});

But if something else occur and the node.js process hang out, there will be some problem with the connections created at the sever. I don't know almost any thing about websockets or signalr. Thanks in advance.

aloysius-pgast commented 5 years ago

Not sure what you mean by node.js process hang out. Connection will be automatically closed if process exits. The only thing which could happen if you don't call client.disconnect manually is that node.js process won't exit until the connection is closed (unless ctrl-c or kill)

hrivera2014 commented 5 years ago

Ok, thanks so much for your willing to help and your time.