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.
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)
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);
@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’ ;-).
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?