Hi Guys, @Dave :
I just tested below API call which will subscribe the event according to the userID(This is mentioned in GitHub - particle-iot/spark-server: UNMAINTAINED - An API compatible open source server for interacting with devices speaking the spark-protocol) .Unfortunately, It didnt get to work.
/v1/devices/events
I found I can received the event by using below api:). But it will not meet my requirment ,because it is receiving all events from all the users...
/v1/events
In hardware side , I coded it as
Spark.publish("my-event",publishString,60,PRIVATE);
In my test , There are only one core and one user account. I have also review the source code both server and protocol, i found the message from hardware seems not meet the logic which deal with the userid ? Below is what found ,can you read it to help me, it maybe not correct, I just trying to known where is my mistake or it is from the system.
The event subscribe logic:
Spark-Server -> CoreController -> the method subscribe will used for register a event to the global publisher which is obviousely appending the userid to the event name.
subscribe: function (isPublic, name, userid) {
if (userid && (userid != "")) {
name = userid + "/" + name;
}
// if (!sock) {
// return false;
// }
//start permitting these messages through on this socket.
global.publisher.subscribe(name, this);
return false;
},
In the event publish logic:
In the spark-protocol -> SparkCore.js-> method named "onCoreSentEvent -- line 1055" , It is publishing a event as below,, but the obj.userid are not taken care in the whole method ?..
onCoreSentEvent: function(msg, isPublic) {
if (!msg) {
logger.error("CORE EVENT - msg obj was empty?!");
return;
}
//TODO: if the core is publishing messages too fast:
//this.sendReply("EventSlowdown", msg.getId());
//name: "/E/TestEvent", trim the "/e/" or "/E/" off the start of the uri path
var obj = {
name: msg.getUriPath().substr(3),
is_public: isPublic,
ttl: msg.getMaxAge(),
data: msg.getPayload().toString(),
published_by: this.getHexCoreID(),
published_at: moment().toISOString()
};
//snap obj.ttl to the right value.
obj.ttl = (obj.ttl > 0) ? obj.ttl : 60;
//snap data to not incorrectly default to an empty string.
if (msg.getPayloadLength() == 0) {
obj.data = null;
}
var lowername = obj.name.toLowerCase();
if (lowername.indexOf("spark") == 0) {
//allow some kinds of message through.
var eat_message = true;
//if we do let these through, make them private.
isPublic = false;
if (lowername == "spark/cc3000-patch-version") {
}
if (eat_message) {
//short-circuit
this.sendReply("EventAck", msg.getId());
return;
}
}
try {
if (!global.publisher) {
return;
}
if (!global.publisher.publish(isPublic, obj.name, obj.userid, obj.data, obj.ttl, obj.published_at, this.getHexCoreID())) {
//this core is over its limit, and that message was not sent.
this.sendReply("EventSlowdown", msg.getId());
}
else {
this.sendReply("EventAck", msg.getId());
}
}
catch (ex) {
logger.error("onCoreSentEvent: failed writing to socket - " + ex);
}
},