Hexagon / node-telldus

Node bindings for telldus-core
Other
34 stars 10 forks source link

Node process exits when it should wait for events... #70

Closed niklaswall closed 8 years ago

niklaswall commented 8 years ago

Hi,

I'm trying to listen to Sensor Events, but the node process exits when it (according to me) should wait for events and fire the callback when the sensors send data (temperature etc.).

Code:

var telldus = require('telldus');

var listener = telldus.addSensorEventListener(function(deviceId,protocol,model,type,value,timestamp) {
  console.log('New sensor event received: ',deviceId,protocol,model,type,value,timestamp);
});

console.log('Event listener set up, waiting for events...');

Node exists with return code 0, so it does not look like it crashes or fail in any other way.

Any idea? (or anything i'm missing?)

jdagerot commented 8 years ago

This is more of a node.js question.

The listener will run your callback code. It's asynchronous so you will need to set up a server that keeps running. I'm using express for example something like this, it's out of my head I haven't tried it.

var app =  require('express')();

// YOUR CODE

var telldus = require('telldus');

var listener = telldus.addSensorEventListener(function(deviceId,protocol,model,type,value,timestamp){
    console.log('New sensor event received: ',deviceId,protocol,model,type,value,timestamp);
});

console.log('Event listener set up, waiting for events...');

// END YOUR CODE

var server = app.listen(8000, function() {
    console.log('Listening on port %d', server.address().port);
});
niklaswall commented 8 years ago

Yes, you are right that it works fine if I rely on "some other" component to keep the instance running... for example express (or just a simple http server).

I was just thinking that since I register a callback event, the node-telldus component should keep the process running until I unregister / remove the callback... basically the same type of behaviour that you have on a http server where you expect http's server.listen() to keep the event loop running while it accepts new connections on the socket.

But you are right, it's not really a bug it's more of a design choice i guess :)

Hexagon commented 8 years ago

Indeed it's a design choice, this library expects something other than itself to keep the process alive :)