How to use reactive vars on the server side

I want to use the server react counter var. But I can’t figure out how to do this without using collections. I expect that {{count}} will be updated after changing the number of var servers without refreshing the page, or how to send the client that the account has been changed?

<body>
  {{> test }}
</body>

<template name="test">
  {{count}}
</template>

customer:

Meteor.call('count', function(err, result) {
  console.log(result)
  Session.set('count', result)
})

Template.test.helpers({
  count: function () {
    return Session.get('count')
  }
});

Server:

var count=0

Meteor.startup(function () {
  Meteor.setInterval(function() {
    count++
  }, 1000)
});

Meteor.methods({
  count: function() {
    return count
  }
})

My MeteorPad Code

I want to see what I expect:

Customer:

Meteor.subscribe('count')

Template.test.helpers({
  count: function () {
   return Counter.findOne().count
  }
});

Are common:

Counter = new Mongo.Collection('count')

Server:

Meteor.publish('count', function() {
  return Counter.find()
})

Meteor.startup(function () {
  if(Counter.find().count() === 0) {
    Counter.insert({count: 0})
  }

  Meteor.setInterval(function() {
    Counter.update({}, {$inc: {count: 1}})
  }, 1000)
});

Meteorpad example

+4
source share
2 answers
0

, , . http://docs.meteor.com/#/full/reactivity - , Session ReactiveVar - , .

(, , , ). , !

0

All Articles