# Particle stops receiving event callbacks

**URL:** <https://community.particle.io/t/particle-stops-receiving-event-callbacks/31094>\
**Category:** Cloud\
**Created:** [March 29, 2017, 3:35pm UTC](https://community.particle.io/t/particle-stops-receiving-event-callbacks/31094 "2017-03-29T15:35:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TechnoX](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/technox/32/12880_2.png) [@TechnoX](https://community.particle.io/u/TechnoX)\
**Post date:** [March 29, 2017, 3:35pm UTC](https://community.particle.io/t/particle-stops-receiving-event-callbacks/31094/1 "2017-03-29T15:35:32Z")

</div>

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:

> [@Event subscription from node client getting dropped intermittently](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021):
>
> Hi, I using the nodejs sample code that listens to all the events coming from my devices(tried both get-event-stream-forever.js and get-event-stream.js). The code seems to work fine for about 30 mins to an hour(duration varies every try, but seems to work for 30 mins without issue everytime), and then suddenly stops receiving events. Any help in this matter will be very much appreciated. /\*jslint node: true \*/ "use strict"; //var spark = require('../../lib/spark'); var spark = require('spark'…

> [@onEvent() listeners stop being called after a few days](https://community.particle.io/t/onevent-listeners-stop-being-called-after-a-few-days/18995):
>
> 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 Photon…

> [@Lost event stream \[solved\]](https://community.particle.io/t/lost-event-stream-solved/9304):
>
> Hi, When subscribing to events using a simple server in Node, the eventstream is lost after some time (usually a few minutes) and the server needs to be restarted. Is there a way to check if the stream is lost somehow? If there is, it would be possible to subscribe again without restarting the process. Some sample code: var Spark = require('spark'); var subscribe = function(){ Spark.getEventStream(false, 'mine', function(data) { console.log(data); }); } Spark.login({accessToken: 'to…

And this git issue:

> <https://github.com/particle-iot/sparkjs/issues/72>
>
> I have a motion detector attached to my spark core, that outputs events like:
> 
> \`…\`\`
> Spark.publish("motion-detected", NULL, 60, PRIVATE);
> \`\`\`
> 
> I'm using this script to monitor it:
> 
> \> https://github.com/dgmltn/pimatic-particle/blob/master/test-eventstream.js
> 
> It stops logging events after a long time (after a day or two). I noticed the same issue when using the EventSource js module instead of the official sparkjs module. I know it's a problem with the client, because if I restart the client script, it continues chugging along.

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

---

<div class="post-metadata">

**Author:** ![TechnoX](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/technox/32/12880_2.png) [@TechnoX](https://community.particle.io/u/TechnoX)\
**Post date:** [April 9, 2017, 11:43am UTC](https://community.particle.io/t/particle-stops-receiving-event-callbacks/31094/2 "2017-04-09T11:43:36Z")

</div>

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);
        });
    });

    */
```

---

<div class="post-metadata">

**Author:** ![dangardiner](https://avatars.discourse-cdn.com/v4/letter/d/5fc32e/32.png) [@dangardiner](https://community.particle.io/u/dangardiner)\
**Post date:** [April 21, 2017, 1:33pm UTC](https://community.particle.io/t/particle-stops-receiving-event-callbacks/31094/3 "2017-04-21T13:33:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![twospoons](https://avatars.discourse-cdn.com/v4/letter/t/bbce88/32.png) [@twospoons](https://community.particle.io/u/twospoons)\
**Post date:** [June 20, 2017, 6:13am UTC](https://community.particle.io/t/particle-stops-receiving-event-callbacks/31094/4 "2017-06-20T06:13:06Z")

</div>

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?
