0

I have built a rolling log table where the most recent 500 events are shown. I do not want it to be reactive because the traffic is too high and it would have just make the screen flash constantly. Instead, I'd like to make the client poll the server every minute.

I have autopublish turned off already and have the server code publish only the last 500 events. The client subscribes to the same channel as usual. Since I do not run a replica set (i.e. no oplog) on my mongodb, the server doesn't really act reactive at all (if I use a regular mongo client to insert new documents then the meteor app would not know about it). I think this is actually working to my advantage since I don't want the reactivity at all anyways.

I think I can use Meteor.setInterval to fetch data every minute but I don't know how to update the client minimongo data cache (or invalidate it so it can ask the server to publish new copy/deltas).

Sorry if this is such a simple question. I'm a newbie with meteor.

Thanks in advance.

fatdragon
  • 2,211
  • 4
  • 26
  • 43

3 Answers3

0

I think client polling is not a good way to send data at intervals. If the only problem with constant update is screen flashing then just make the Template helper update at intervals.

A rough example of client code:

Meteor.setInterval( function(){
    Session.set('refresh', new Date());
}, 1000);

Template.whatever.helpers({
  logs: function(){
    Session.get('refresh');
    return Logs.find({},{reactive:false});
  }
 });
user728291
  • 4,138
  • 1
  • 23
  • 26
0

Suppose you're using Meteor.Collection, according to Meteor's documentation, You can turn off reactivity of your query by:

your_collection.find({}, {reactive: false})

then when you want to update your results, call it again. In your case, call it per minute.

JK ABC
  • 576
  • 5
  • 12
  • Mmm... I actually tried that before. The problem is that while the collection isn't reactive, I was unable to issue a find() call from the client again and make the minimongo cache refresh i.e. it keeps returning old data. I'll try other solutions below and update everyone. Thanks for your help. – fatdragon Jul 14 '14 at 04:16
-1

If you feel meteor can't handle the load of this it might not be worth using Meteor for your use case. Meteor's front end (Blaze) can be used externally without Meteor: https://github.com/meteor/blaze

The reason I say this is the DDP socket will still be open. Nonetheless if you want to keep it this way you can use a Meteor.call and Meteor.setInterval

Server side

Meteor.methods({
    "yourdata" : function(data) {
        var somequery = MyCollection.find().fetch()

        return somequery
    }
});

Client Side

Meteor.setInterval(function() {
    Meteor.call("yourdata", function(err, result) {
        Session.set("results", result);
    });
}, 60000 /*1 minute poll*/);

Then in your helpers

Template.yourTemplate.helpers({
    results: function() {
        return Session.get("results");
    }
});

Then you can use {{#each results}} to display your data as before.


Why this is a bad idea

While you can do this using the code above, if it is your choice:

Keep in mind if there is NO change to your database then your overhead using polling this way is far worse than using the ordinary Meteor.publish, especially if you're using the oplog.

This is because you would be querying the database every minute, whereas with ordinary publish methods you would not have made a single call to the database (since there is no change)

Its a bit like checking for email every 20 minutes or so or using push notifications on your phone. If you don't get any email its a bit of a waste to check every 20 mins and it would always be faster to get the push notification.

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • `600000 /*1 minute poll*/` I think you like zeroes a little bit too much ;P – Peppe L-G Jul 11 '14 at 07:39
  • Your method is called `yourdata`, and not `somequery`. – Peppe L-G Jul 11 '14 at 07:42
  • In my situation, the database changes constantly and relatively quickly. From a usability perspectively, I wouldn't keep the client updated constantly because otherwise it would look like a "tail -f" window :-) Thanks for your detailed feedback. I'll give it a try. – fatdragon Jul 14 '14 at 04:13