Message Boards Message Boards

Building reactive, multiuser countdown clocks in WL

Posted 9 years ago

Is it possible to create WL applications that either listen to websockets, do long polling of a server, or possibly respond to server sent events?

I have a very simple test case. I would like to build a countdown timer that can be viewed on lots of web browsers, but would be controlled by a WL app running on an iWatch (you can tell I was inspired by a recent video!).

In essence, the user would set a time on the watch, and click start. All the connected browsers would then show the same time, as they count down. Clicking pause on the watch would instantly (< 1s) stop all the clocks. Building these sorts of apps is trivial using e.g. Node JS, or similar stacks, but I would love to build it in WL.

Is this type of multiuser interactivity possible?

POSTED BY: Andrew Burnett
2 Replies
Posted 9 years ago

Hi Marco,

Thanks for taking the time to work this out. That seems like a very neat solution. I will have to give it a try on the iWatch.

Cheers Andy

POSTED BY: Andrew Burnett

Hi,

this is far from ideal, but it does something in the lines of what you want.

First you create a Databin and make that publicly available:

binstarttime = CreateDatabin[];
SetOptions[binstarttime, "Permissions" -> "Public"];

The data bin will have a short ID something like 4LilXAIZ; you obviously need to change that to your Databin's ID. This following command sets the count-down to 5 minutes:

DatabinAdd[Databin["4LilXAIZ"], {Now, Quantity[5, "Minutes"]}]; 

On each of the clients you run:

RunScheduledTask[dummy = Databin["4LilXAIZ"]["Latest"]["Data"];, 1];
Dynamic[Refresh[
  If[dummy === {0, 0}, "Stop", 
   If[QuantityMagnitude[
      UnitConvert[dummy[[2]], "Seconds"] - 
       UnitConvert[Now - dummy[[1]], "Seconds"]] < 0, "Time's up!", 
    UnitConvert[dummy[[2]], "Seconds"] - 
     UnitConvert[Now - dummy[[1]], "Seconds"]]], 
  UpdateInterval -> 0.01]]

That should initiate the countdown. If you or anyone wants to stop the countdown this works:

Quiet[DatabinAdd[Databin["4LilXAIZ"], {0, 0}];]

When you are done you should also remove the Scheduled task:

RemoveScheduledTask[ScheduledTasks[]]

This is, of course, not really optimal. The RunScheduledTask slows the display of the count-down down. (That sounds funny!). Anyway, I cannot improve it any further right now as I am invigilating an exam and we are nearly doneĀ…

Cheers,

Marco

PS: If you can live with "distributing" a potential "Stop" with up to five seconds delay, this would work instead of the RunScheduledTask above:

RunScheduledTask[dummy = Databin["4LilXAIZ"]["Latest"]["Data"];, 5]

Then, the clock is much smoother and only stalls a bit every 5 seconds.

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract