Suddenly error with Publish() [SOLVED]

Hi, after working fine for more than 2 weeks I suddenly (just a few hours ago) got problems with the connection of my NodeJS server and the SparkCloud.

After starting the NodeJS server I get the following error in the console:

undefined:1
Connect Error
^
SyntaxError: Unexpected token C
    at Object.parse (native)
    at EventSource.request.uri (/Users/henkgescher/DuikelDevelopment/DuikelServer/duikelServer.js:24:27)

The relevant code on the NodeJS server is (line 24 is the last line of the shown code):

var EventSource = require('eventsource');
:
var accessToken = "xxxxmyaccesstokenxxxxxx";
var eventSource = new EventSource("https://api.spark.io/v1/events/?access_token=" + accessToken);
:
eventSource.addEventListener('Log', function(event){    // Wait for publish event
    var rawData = JSON.parse(event.data);               // Parse the main JSON data 
    var parsedData = JSON.parse(rawData.data);          // Parse the sub JSON data
:  

I first suspected my NodeJS but my collegue called me that he has exactly the same error on another system.
So therefore I think something has changed in the SparkCloud.

Any idea how to solve this error?

Thanks,
Henk

HI @nika8991

I think it is likely that your access token has expired and there are a bunch of ways to get a new one.

If you login to the web IDE and click on the gear icon, it will tell you for sure.

Hi @bko, that was also my first reaction, I refreshed the accesstoken, unfortunately it did not solve the problem.

Thanks,
Henk

Can you post the raw output you receive from the Spark.publish() event? It could be some malformed JSON causing it to choke up on that line.

Hi @wgbartley,
Here the JSON format in case of the error.
It is strange that we receive this publish message because the coreid is not ours!
I now understand what goes wrong because we expect a nested JSON format (JSON in JSON, see below in case it goes well)

DuikelServer version: 0.0.0.4
{"data":"Connect Error","ttl":"60","published_at":"2014-06-23T08:29:31.279Z","coreid":"55ff70065075555352141787"}

undefined:1
Connect Error
^
SyntaxError: Unexpected token C
    at Object.parse (native)
    at EventSource.request.uri (/Users/henkgescher/DuikelDevelopment/DuikelServer/duikelServer.js:25:27)

Here the JSON format in case it works well:

DuikelServer version: 0.0.0.4
{"data":"{ \"T\": \"000835\", \"N\": \"State\", \"V\": \"START\" }","ttl":"120","published_at":"2014-06-23T07:13:07.513Z","coreid":"53ff6b065067544813410487"}
CoreId: 53ff6b065067544813410487
Time: 2014-06-23T07:13:07.513Z
1 Time: 000835 State: START
{"data":"{ \"T\": \"000850\", \"N\": \"Version\", \"V\": \"0.2.0.15\" }","ttl":"120","published_at":"2014-06-23T07:13:08.512Z","coreid":"53ff6b065067544813410487"}
2 Time: 000850 Version: 0.2.0.15
: 

Thanks,
Henk

Hi @nika8991

You are seeing everyone’s events because you API URL does not have a devices field and someone started publishing a public event with the same name.

Try this:

var accessToken = "xxxxmyaccesstokenxxxxxx";
var deviceID = "53ff6b065067544813410487";
var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);

Hi @nika8991,

If you want to receive events from just your cores, try watching your personal device firehose instead of the global firehose here:

https://api.spark.io/v1/devices/events/

Thanks!
David

1 Like

@Dave and @bko, I the beginning I had the deviceID included but I removed that because I want to receive the publish messages from all my SparkCores in the NodeJS server in a common function. Therefore I removed the deviceId assuming that the accessToken should take care that I only receive my own publish messages but that seems not to be the case.

I can solve it by checking the received deviceID, if it is one of my own then continue, otherwise disgard the published message. I also changed the name of the publish message to be more unique in stead of ‘Log’ ;-).

Thanks,
Henk

Uhm, still don’t understand the problem.

We did some tests, my colleque has accessToken1 and a few cores registered with that accessToken1, I have accessToken2 and some other cores.
We both have a NodeJS server running, his server has subscribed for publish messages with his accessToken1, my server has subscribed for messages with accessToken2. If his cores publish messages his server receives them, my server not. The same happens when my cores are publishing, my server shows the messages, his server not. All our cores use the same publish name. This is what we expected.

The strange things is still: why did we see publish messages from a for us strange coreID?

Thanks,
Henk

Hi @nika8991

There are a bunch of good ways to use the URL scheme for the Spark API

  • The full public event stream, filtered only by event name:
    https://api.spark.io/v1/events/?access_token=<<token>>

  • The event stream for all of your cores:
    https://api.spark.io/v1/devices/events/?access_token=<<token>>

  • The event steam for a single core:
    https://api.spark.io/v1/devices/<<deviceID>>/events/?access_token=<<token>>

3 Likes

Hi @bko,

Thank you, now it is clear to me :smile:
I will test the second alternative, that is the one I need.

Henk.

2 Likes