0

Hey hava googled and tried every thing - but nothing seems to work, so now i will ask you guys:

How do i make my Node app. from stop exiting - when i like it to keep running and wait for response from a Web Socket connection? As you can see i have tried different stuff in the 'PreventExit' function. I simply want the app. to keep receiving data from the web socket connection - without closing/exiting.

var Gdax = require('gdax');
var WSemitter = require('../eventemitters/websocket-emitter');

module.exports = {
    initws: function () {
        var WebSocketEmit = new WSemitter();

        test();

        function test() {
            try {
                PreventExit();

                var websocket = new Gdax.WebsocketClient();
                websocket.on('message', function (data) {
                    console.log(data);
                    WebSocketEmit.fakeEvent();
                });
            }
            catch (err) {
                console.log(err);
            }
        }

        function PreventExit(){
            WebSocketEmit.on('fakeEvent', function () {
                console.log('Just fake - to keep node running!');
            });

            setTimeout(function(){}, 100000);
            process.stdin.resume();
        }
    }
}

UPDATE1:

My Web Socket provider CoinBase has recently updated their API. From Coinbase-exchange to Gdax. I just tried to replace:

var Gdax = require('gdax');
var CoinbaseExchange = require('coinbase-exchange');

//var websocket = new Gdax.WebsocketClient();
var websocket = new CoinbaseExchange.WebsocketClient();

and now my app waits! But after only 1 received json object my node app still exits!?? How do i make it keep the coonection open and not just shut down? But instead keeps receiving data from the API?

Update 2

When i remove all my test code and ONLY have the code related to the API, it works - BUT only with the old one...coinbase-exchange?? So it must be an error in there new (gdax)API or what? But no exception is thrown??

Please see the doc here:

https://www.npmjs.com/package/coinbase-exchange#websocket-client

https://www.npmjs.com/package/gdax#websocket-client

Further the DOC states (Which makes no sense - when it is only the old API which is the one still working):

GDAX transition As part of the rebranding of Coinbase Exchange to GDAX, API endpoints have changed. The old *.exchange.coinbase.com endpoints will continue working until Aug 24th, 2016. For the new endpoints see the documentation below.

PabloDK
  • 2,181
  • 2
  • 19
  • 42

1 Answers1

2

Using setTimeout

You can do something like this - create a timeout that re-registers itself:

var t;
function nt() {
  t = setTimeout(nt, 10000);
}
nt();

Every time you can turn it off by calling clearTimeout. For example to make your program exit after 2 seconds you can add:

setTimeout(function () {
  clearTimeout(t);
}, 2000);

So it doesn't even have to wait for those full 10 seconds (or whatever is the interval) to finish.

Using setInterval

You can make it even simpler with setInterval:

var i = setInterval(()=>{}, 10000);

And you can cancel it with clearInterval:

setTimeout(function () {
  clearInterval(i);
}, 2000);

Real problem

But if your process exits then there's a good chance that it does so because it no longer waits for anything to happen and that's the real problem. So keeping Node from exiting may not fix your problem in that case - it will just wait for nothing to happen.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • I have already tried with an setInterval function... in my PreventExit function... but it doesnt change any thing at all... all this seems to do the same - nothing...? – PabloDK Nov 30 '16 at 12:24
  • `setInterval` solution works just great, the only thing is that I enclose the code into `if(1){...}` in Node REPL - another lifehack found here. – Vadim Anisimov Nov 06 '18 at 06:12