Spark.onEvent() Instability?

I’m hoping someone may be able to provide some advice. I’m attempting to register a listener for an event within a Node/Express app. Everything functions fine at startup, but typically within an hour of functioning the node app stops responding. I’ve gone a bunch of googling but I can’t find anything mentioning any known instability or memory leaks in the event subscription code. I guess one possibility is that it isn’t intended to be used on the server side and that long running “subscriptions” will not be viable, but that seems silly to me.

Here’s my code:

// Setup Particle.io communications
var spark = require('spark');

// Subscribe to bathroom notifications
spark.on('login', function() {
  spark.onEvent('bathrooms', function(data) {

    // Sample data: {"data":"not_occupied","ttl":"60","published_at":"2015-09-02T04:31:18.577Z","coreid":"samplecoreid","name":"bathrooms/Bathroom1"}
    var bathroom_name = data.name.substring(10);
    var bathroom_status = (data.data == "occupied" ? true : false);

    // Get a Postgres client from the connection pool
    pg.connect(connectionString, function(err, client, done) {
        // SQL Query > Update Data
        client.query("UPDATE restrooms SET occupied = $1 WHERE name = $2", [bathroom_status, bathroom_name]);
        // Handle Errors
        if(err) {
          console.log(err);
        }
    });

    // Send updates to listening browsers through the socket connection
    io.emit('occupancy_update', { bathroom: bathroom_name, occupied:bathroom_status });

  });
});

// Login as usual
var promise = spark.login({ username: process.env.PARTICLE_USER, password: process.env.PARTICLE_PASS });

Any thoughts? Your help would be appreciated, I’m banging my head against the wall.

I’ll venture a guess. onEvent creates a long running connection to the Particle cloud server and receives events on that connection. When that connection breaks for any reason you need code to reconnect.

The onEvent call returns the actual request. You can look at https://github.com/request/request to figure out how to handle errors.

My guess is:

var request = spark.onEvent('bathrooms', function(data) { /* process events */ });
request.on('error', function(err) { /* reconnect */ });