# Event subscription from node client getting dropped intermittently

**URL:** <https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021>\
**Category:** Troubleshooting\
**Created:** [August 1, 2015, 4:36pm UTC](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021 "2015-08-01T16:36:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravi](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@ravi](https://community.particle.io/u/ravi)\
**Post date:** [August 1, 2015, 4:36pm UTC](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021/1 "2015-08-01T16:36:42Z")

</div>

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.

```auto
/*jslint node: true */
"use strict";

//var spark = require('../../lib/spark');
var spark = require('spark');

spark.on('login', function() {

  spark.getEventStream(false, 'mine', function(data) {
    console.log("Event: " + JSON.stringify(data));
  });

});

// Login as usual
spark.login({ username: 'xxxxx', password: 'xxxxx'});

```

Thank you

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [August 1, 2015, 4:55pm UTC](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021/2 "2015-08-01T16:55:48Z")

</div>

The get-event-stream-forever.js works fine for me. It only fails if my internet drops, otherwise, it’ll re-establish a connection. Is there an error message when it fails? That should make it easier to debug.

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [September 2, 2015, 2:20am UTC](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021/3 "2015-09-02T02:20:09Z")

</div>

Dear @Moors7  
I’m using the same code (get-event-stream-forever). I’d like to ask you if there is any way that I can re-establish a connection if my internet drops?  
Thanks in advance.  
Ahmed

Here is the code:

```auto
/*jslint node: true */
"use strict";
// Setup the modules we need to use
var spark = require('spark');
var fs = require('fs');
var d = 0;
var nd = 0;
var utc = 0;
// Login and start doing things!
var openStream = function() {
	//Get the locale time for Columbia instead of UTC/GMT
	function calcTime(city, offset) {
		d = new Date();
		utc = d.getTime() + (d.getTimezoneOffset() * 60000);
		nd = new Date(utc + (3600000 * offset));
		return " Published at " + nd.toLocaleString();
	}

	//Get test event for specific core
	var req = spark.getEventStream('Spark1', '000000000000000000', function(data) {
		console.log("New Event: " + JSON.stringify(data) + ". Logging it to file");
		fs.appendFile('Spark.txt', data.data + "," +
			calcTime('Columbia', '-5.0') + "," + data.name + "\r\n", function(err) {
			if (err)
				console.log("File log failure. Error = " + JSON.stringify(err));
			else
				console.log("File log success");
		});
	});
	req.on('end', function() {
		console.log("ended! re-opening in 3 seconds...");
		setTimeout(openStream, 3 * 1000);
	});
};

spark.on('login', function() {
	openStream();
});
// Login as usual
spark.login({ username: 'myemail@yahoo.com', password: 'mypassword'});

```

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [September 2, 2015, 2:23am UTC](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021/4 "2015-09-02T02:23:38Z")

</div>

Any suggestion please @ScruffR @harrisonhjones  
Thanks in advance.  
Ahmed

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [September 3, 2015, 4:42pm UTC](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021/5 "2015-09-03T16:42:41Z")

</div>

Hmm, I haven’t tested this locally, but you could watch for more events on the api request object, error might be good, I’m surprised ‘end’ isn’t being called, but you could try something like:

```auto
req.on('error', function() { /* reconnect stuff here */ });

```

Otherwise you could make an assumption like, "hey I should be getting at least 1 event every five minutes, so reconnect if that’s not true, etc.

for example:

```auto
	//Get test event for specific core
	var req = spark.getEventStream('Spark1', '000000000000000000', function(data) {
                // record the time
		lastEventTime = new Date();

...
//snip!
...

setInterval(function() {
	var elapsed = (new Date()) - lastEventTime;
	if (elapsed > (5*60*1000)) {
		//it's been more than 5 minutes!
		console.log("ended! re-opening in 3 seconds...");
		setTimeout(openStream, 3 * 1000);
	}
}, 30*1000);

```

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![Ahmedsa1983](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ahmedsa1983](https://community.particle.io/u/Ahmedsa1983)\
**Post date:** [September 15, 2015, 12:35pm UTC](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021/6 "2015-09-15T12:35:12Z")

</div>

Thanks a lot @Dave for your reply. I have two question here. Firstly, what is the role for this statement?

> [@Dave](#):
>
> }, 30\*1000);

And, do I need to change this line if I decided to reduce the 5 minutes to 1 minute for example.

Secondly, is it correct to define lastEventTime as a variable like this?

> [@Dave](#):
>
> var lastEventTime = new Date();

Finally, I started testing this code to see if it will reconnect.  
Have a great day.  
Ahmed.

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [September 16, 2015, 1:01am UTC](https://community.particle.io/t/event-subscription-from-node-client-getting-dropped-intermittently/14021/7 "2015-09-16T01:01:02Z")

</div>

This line:

> [@Ahmedsa1983](#):
>
> }, 30\*1000);

sets the delay for an interval callback. That callback checks the heartbeat clock to see if it's been more than 5 minutes. So that means, every 30 seconds we check to see if it's been longer than 5 minutes. 🙂

Yes that's the right way to declare lastEventTime, just make sure it's scoped appropriately so the code can find it.

Thanks!  
David
