How to get the server data push to the client instead of page refresh?

Also in future specific users can see only specfic markers which they are allowed to see or a user queries the a devId (marker) and get the currnet latlon position for that devId and hence our collection will be a group of devId and latlon where the devId will be unique.

Here’s a gist for non-persisted data in a reactive Meteor pub/sub. This is really stripped down to the basics, but hopefully it’ll explain what’s going on.

In client/RandomNumbers.js:

RandomNumbers = new Mongo.Collection('randomNumbers');

Note, this is a client-only collection. We’re going to generate data on the server to publish into this.

In client/template.html:

<body>
  {{> randomNumbers}}
</body>

<template name="randomNumbers">
  {{#each randomNumber}}
    <div>
      The latest random number is: {{latest}}
    </div>
  {{/each}}
</template>

In clients/template.js:

Template.randomNumbers.onCreated(function() {
  this.subscribe('randoms');
});

Template.randomNumbers.helpers({
  randomNumber: function() {
    return RandomNumbers.find();
  }
});

You’ll notice that we’re subscribing to the randoms publication. The template helper does a standard find on the collection.

In server/server.js

Meteor.publish('randoms', function() {
  var init = false;
  var self = this;
  Meteor.setInterval(function() {
    var ran = Math.floor(Math.random() * 101);
    if (init) {
      self.changed('randomNumbers', 'dummykey', {latest:ran});
    } else {
      self.added('randomNumbers', 'dummykey', {latest:ran});
    }
    self.ready();
    init = true;
  }, 1000);
});

This is where we publish the randoms data. The data is intended for a mini-mongo collection, so will need an _id. As we want a single, updating document, we need to ensure we know (for re-use) the key. Hence the _id is defined as 'dummykey' (this can be anything, as long as we know it and it’s unique). On first run, we perform a self.inserted() to create the document. Subsequently, we just do a self.updated() to change the payload (random number). The document we publish will just contain _id and latest. Of course, this could be as complex as we want.

Does this help?

[EDITED for typos]

1 Like

OK, our posts crossed. My gist should be enough to get you started, but it sounds as though you may need persisted data, and you will certainly need to include the userid somewhere to ensure each user only gets to see their stuff.

As you can see from my code above. The find from the client is empty. I can see the mongo db get the published collection but on the client the collection returns empty.

I have given the publish code under the startup function in the server. Will that be an issue?

In my old way I would have implemented auto refresh on the client side and all the markers would have fetched the latest lat lon value from the api and usinh ajax only the marker would have reloaded with the latest lat lon. But in meteor I am needed to persist the data and use cron job to refresh the latest lat lon by calling the api. Is there a simple way to do this in meteor?

I am able to get it working now by adding something this
Template.randomNumbers.onCreated(function() {
this.subscribe(‘randoms’);
});

now I will focus on getting the lat lon on a marker

Hi I am getting the following error when i try to use markerClusterGroup. Can you pls help?

Exception from Tracker afterFlush function:
debug.js:41 TypeError: L.markerClusterGroup is not a function
at Template.tracking.rendered (tracking.js?ded05ecc021eca44bdd75ac1ea57ec5b569531e2:47)
at template.js:116
at Function.Template._withTemplateInstanceFunc (template.js:437)
at fireCallbacks (template.js:112)
at null. (template.js:205)
at view.js:104
at Object.Blaze._withCurrentView (view.js:523)
at view.js:103
at Object.Tracker._runFlush (tracker.js:468)
at onGlobalMessage (setimmediate.js:102)

Maybe this very easy TimerTicker Example will help you

http://meteorpad.com/pad/jYDG3wjryGc6DHXa6/Sample_Publish_to_Local-Collection

http://meteorpad.com/pad/i7ec92p45kswPF89E/Sample_Publish_to_Local-Collection_via_observe

Man, Meteor is really simple! Just give a try for docs.meteor.com. Everything you need have been discribed in docs and samples. Digg it a little and you’ll find parties example

I think, you didn;t add plugin markerCluster… (-:

yeh. i have added and it worked. Thanks