javascript - Heartbeat method in Meteor -


i have meteor application that, every second while user holding button, decrements value on post 1. there many many buttons, , each should affect itself.

if 1 person holding button, go down rate of 1 per second. if 2 people (on different clients) 2 per second.

the exact moment @ updated in database unnecessary, client should see counter decrement variable amount each second.

so trigger method used following

template.button.onrendered(function() {   var data = this.data;    meteor.setinterval(function() {     if(isclicked) {       meteor.call('heartbeat', data);     }   }, 1000); }); 

which calls client/server method

'heartbeat': function(button) {   buttons.update({_id: button._id}, {$inc: {life: -1}});    if(button.life <= 1) {     console.log('remove dead buttons');     buttons.remove({life: {$lte: 0}});   } } 

and causes weird activity when latency comes play. when click on button, life goes down predictably @ 1hz on client. on different client, life might stand still several seconds, , decrement in chunks of 2 4 until catches up. supposed jump new value hears server it's been updated, rather ramping down.

but since there no code make ramp, led believe there more fundamentally wrong in play. there 3 states button goes through:

client decrements value -> server decrements value -> client b reads value 

the problem seems between client , server, perhaps setting interval call method every second bottleneck here need fix, i'm not sure of elegant solutions. if instead of sending each update, adopted dead reckoning approach , send start , ends , had server interpolate on each second in between, might alleviate issues, don't know if i'm going run more unexpected issues along line.

is there better way of decrementing counter on second every second every client reactively?

an alternate approach:

  1. when user presses button (catch in template event handler), make meteor.call() server , have server decrement every second.
  2. when user releases button, make meteor.call() cancel decrementing. you'll need keep handle setinterval can cancel it.

this way you'll remove client->server lag doing updates.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -