Help understanding push data versus polling

I am writing a game based on @BDub's pinewood derby code

What I am wanting to do is control it via a webpage. I easily figured out how to trigger the round by calling a cloud function via POST. I can also get the time variables at the end of the round by using GET.

I dont know when the round ends, it could end at any point. My question is, how can I get it to push the end of the round data to the webpage instead of having to constantly poll it with GET?

I think Spark.publish() would be ideal here. You don’t really even need the data to be published, just an event that says “I’m done and you can read my variables now!”.

Well @bko, you beat me to it. I was going to link to your tutorials anyhow, so I guess that doesn’t matter much :wink:
If you’re publishing anyhow, why wouldn’t you use that data directly. Seems to be an unnecessary step to me to say:“hey, I’ve got a live update containing data, let’s actively poll for that same data.” If you’re sending that stuff over the web, you might as well just use it. This however only hold true if the data is small enough to fit within a publish. If you’re polling a lot of variables, the “notify to poll” seems reasonable.

1 Like

ooh @bko has tutorials? I wanna see!

yes, publishing the results themselves makes sense versus as a trigger to poll

Hi @scsc_tech

There are a lot of tutorials here in the forum:

https://community.spark.io/category/project-share/tutorials

I wrote several on publishing and getting and setting variables.

I mentioned that you wouldn’t have to publish the actual data just to avoid more code changes since you are starting from code that uses variables. Publishing the data works great!

2 Likes

Thanks! to publish the double I would have to use sprintf in some fashion to convert it to a string correct? Im new to it

Yes, that’s right, sprintf() or something similar would be required to convert the double value to a char array string.

Thanks! this was super easy to get working thanks to your tutorial

double tempTime1;
char target1Time[10];
sprintf(target1Time, "%f", tempTime1);
Spark.publish("round/target1",target1Time);

the call

curl -H "Authorization: Bearer [MYAPI]" \
    https://api.spark.io/v1/events/round
1 Like