Reputation: 614
Meteor app, typical pattern, I have publish on a server, subscribe on a client.
Reactivity is great, but now I have a need to let client synchronize its local minimongo (or, lets say, fetch new values from server) only each, lets say, 30 seconds.
Is there a way to do so? In other words I must be able to delay synchronisation for n seconds and repeat it every n seconds also.
The only pattern comes in mind right now is a very dirty one - just use an another helper for layout that only updates each n seconds, but that doesn't save me traffic because synchronisation will happen anyway, I will only visually make it like its been synchronized not in real time.
Upvotes: 1
Views: 68
Reputation: 4009
another approach is to add a updateTimestamp
to each document. Then you can publish all the documents until a specific time-stamp and update this every 30 seconds. Making sure you do not get the documents every time they are added or changed
the biggest difficulty would be to manage the time difference between the client and the server.
Meteor.publish("allPosts", function(until){
return Posts.find({updateTimeStamp: {$lte: until}});
});
and on the client
Meteor.setInterval(function(){
Meteor.subscribe("allPosts", new Date());
}, 30000)
Upvotes: 0
Reputation: 162
Seems like you don't necessarily want to prevent the subscription itself from stopping/starting (this would get difficult as meteor will think there is no data and will remove everything reactively).
Really you just want to prevent the UI from updating as often. One way to do that is the following, which will change the local cursor query to be temporarily reactive (allowing the DOM to update) every 5 seconds, and then non-reactive right away:
# client.coffee
Meteor.setInterval ->
Session.set('reactive', true)
Session.set('reactive', false)
, 5000
Template.test.helpers
docs: -> Collection.find {}, {reactive:Session.get('reactive')}
This would be my initial approach just to demo the concept, and it seems pretty hacky; it works in a tiny app but I haven't tested it in anything big. I've never seen this kind of thing being used in a real app, but understand why you might want it.
Upvotes: 2