Particle stops receiving event callbacks


#1

Dear community,

I have been developing a system for RFID activation of equipment. It works flawless for 1-2 days but after that I stop receiving events from the particles (actually I use the old spark core, but should upgrade to photons). I can see the events in the console and the mobile app (particle app), and I can also send data to the particles, so they are in fact connected to the cloud.

I have tracked down the problem to

    particle.getEventStream({ deviceId: 'HIDDEN', name: 'TagAccess', auth: token }).then(function(stream) {
        console.log("Setup tag stream listener");
        stream.on('event', function(e) {
            var data = JSON.parse(e.data);
            // Do other stuff
        });
    });

That stops receiving calls to the callback in stream.on().

I have searched the community and found these relevant topics:




And this git issue:

But they are all concerning the old spark API. In the old API the getEventStream() seems to return a request object where you can do the on(‘end’, callback) call. But what do the new particle getEventStream() return?

For me it seems like you get the stream object in a callback, so I tested the following code:

var openStream = function() {
    //Get your event stream            
    particle.getEventStream({ deviceId: 'HIDDEN', name: 'TagAccess', auth: token }).then(function(stream) {
        console.log("Setup tag stream listener");
        stream.on('event', function(e) {
            var data = JSON.parse(e.data);
            // Do some cool stuff
        });
        stream.on('end', function() {
            console.log("Spark event stream ended! re-opening in 3 seconds...");
            setTimeout(openStream, 3 * 1000);
        });
    });
};

openStream();

But today I noticed that the system stopped getting callbacks again, just as before. So I suspect this is not correct?
How am I supposed to do it?

Why do I even need this restart code? For me it seems like a design error somewhere. It should be able to get the events regardless of what happens?

FYI, code is running on a Raspberry 3. All other code is still active and up-and-running. Just this callback stops getting called.

Best Regards,
Fredrik Löfgren


#2

Done more testing.

The following code has been up and running for over 2 days and transmitting event every 10 minute. But when uncommenting the getEventStream-code below it locks up after about 6h (sometimes 2h and sometimes 10h). For me it feels like a race condition in the event loop inside node when using getEventStream, but I may be wrong.

This is a fully functional file that works by just pasting it and changing your auth-token. Anyone else that experience the same behaviour?

    var Particle = require('particle-api-js');
    var particle = new Particle();
    var token = "SECRET-CHANGE-IT-TO-YOUR-TOKEN";

    var interval = setInterval(function() {
        console.log("internval run ", (new Date()).toLocaleString());
        particle.publishEvent({ name: 'TagAccess', data: "{\"tag\": 123456789, \"code\": \"1337\"}", auth: token}).then(
        function(data) {
                console.log("before data checked: " + JSON.stringify(data.body));
                if (data.body.ok) {
                    console.log("Event published succesfully")
                }
            },
            function(err) {
                console.log("Failed to publish event: " + err)
        }
        );
    }, 1000*60*10);

    /*
    //Get your event stream
    particle.getEventStream({name: 'TagAccess', auth: token }).then(function(stream) {
        console.log("Setup tag stream listener: ", (new Date()).toLocaleString());
        stream.on('event', function(e) {
            console.log("got tag ", (new Date()).toLocaleString());
            var data = JSON.parse(e.data);
            var tag = data.tag;
            var code = data.code;
            var device = e.coreid;
            var date = e.published_at;
            console.log("Tag: ", tag,code,device, date);
        });
    });

    */

#3

I’m seeing that exact same behavior in the c# library (no events after a few hours, but the event still getting published and visible on the Console) - I’ve tried re-authenticating periodically, as well as un- and re-subscribing, but I still stop getting events after a number of hours, until I restart the windows service hosting the application - then it works fine. I’ll let you know if I make progress.


#4

I’m in the same boat with a C# app using the windows SDK.
Runs for a few hours to a day then the event stream halts. Logging out and in doesn’t fix it (I set this to happen automatically on a 6 hour timer). I have to restart the app to get the events stream back.

Any solutions?