onEvent() listeners stop being called after a few days

Hello! I have a server written in Node deployed to a DigitalOcean server. For some reason, after a few days (generally 3-7+) from when the server starts up, the onEvent() callbacks stop being called when an event happens on the Photon.

I can still see the event registered on the Particle Dashboard (so the Photon is definitely connected to the internet and definitely sending the event). However, the Node callback isn’t called anymore. I can use the server’s UI to call cloud methods on the Photons without a problem. The problem is specifically that the server stops receiving callbacks.

This has been happening consistently for a couple months. Any idea how I might be able to fix it?

This might work:

1 Like

Hmm… It looks like that gives access to all events from all cores instead of allowing me to listen to certain events. Obviously I can do event routing inside the callback, but it seems a bit silly when onEvent() is provided.

Is the event stream what’s used in the background anyways? e.g. if I add in the event stream refreshing, will onEvent() calls continue to work automatically? If not I’ll just move my event logic to within the callback.

Regardless: This should probably be clearly stated in the documentation next to onEvent(), subscribe(), etc.

Hi @mvd7793

I am not sure I exactly understand your question. The code that @Moors7 pointed you to basically says, “if the stream closes, reopen it.” which seems like exactly what you need.

Maybe the part that is not clear is that the event stream also sends non-event keep-alive characters just to keep the connection alive, but things can happen on the Photon, or on the internet (routers etc.) or on your host with the event listener. If your Node code does not see a keep-alive or an actual event for many seconds, it should run the “reopen steam” code via the end trigger.

The specificity of the URL you use to access the stream does not really matter here.

Hello! Sorry for the misunderstanding. To clarify my question a bit, my code currently looks like this:

var spark = require('spark');

// insert spark login code

function onSparkLoginComplete() {
    spark.onEvent('myEventName', function(err, data) {
        // This stops being called.

My previous question was about spark.getEventStream() vs. spark.onEvent(). The connection between these isn’t mentioned anywhere in the documentation. I just looked at the source code and I see it’s a simple abstraction. I’ll flip my code around to use getEventStream() instead where I can refresh things manually myself. That answers my question, thanks for the GitHub pointer @Moors7! :slight_smile:

For what it’s worth: It seems impossible to have an onEvent() persist when one of those connection interruptions occurs. That’s not mentioned in the documentation for either spark.onEvent() or device.subscribe(). From a developer perspective, it’s quite unintuitive for a library’s callback to stop working without any documentation around the problem. I would normally assume that the library takes care of it.

I think a link to the persistent stream example should be added to getEventStream(), and a quick snippet suggesting to use getEventStream() for persistence should be added to each of onEvent() and device.subscribe().

1 Like