Set Interval for Meteor.Subscribe to collection?

I have a collection called Players.

In Meteor.Publish, I am running some logic on what data gets returned.

If This Players X is Greater than 10… return this data. Great.

But, because I have logic in there, I found it necessarry to re-subscribe to this data every 1 second to get it working. What am I missing?

Meteor.publish('players', function(PARAM_1, PARAM_2, PARAM_3) {
console.log("-Finding SHIPS near: "+this.userId);
				
				var player_ship = Posts.findOne({type:"ships",parent_id:this.userId}); 
				
				var range = 10;
				var scanner_x_min = player_ship.x - range;
				var scanner_x_max = player_ship.x + range;
				console.log("SCANNING X MAX: "+scanner_x_max);
				
				return Posts.find({ type:"ships", x:{ $lte:scanner_x_max} });	
setInterval(function(){
			// Scanner
			Meteor.subscribe('posts', 'ships' ); 
			
		},1000);

Server-side isn’t reactive, so you’ll need to put your logic in the subscribe call, and probably wrap it an an autorun. If you don’t need ‘realtime’, then maybe swap to a method and call it every X seconds.

I definatley need real time.

I don’t know enough about autorun, I’ve been putting that off lol…

But this actually appears to be the correct way to do it then, yes?

I tried this, so I have a cron job that create a dummy Random.id() every 20 seconds that I passed on my subscribe call…

Meteor.subscribe('posts', getDummyId.get() );

However my problem was it also refresh my page automatically… My data comes from external source.

Do you know how not to refresh it like if I was using the MONGODB?

Thanks

is this a running code already?

I’d recommend:

  1. Using observe http://docs.meteor.com/api/collections.html#Mongo-Cursor-observe (or observeChanges if that works better for a particular scenario)

  2. If observe doesn’t work for some reason then use a poll-based solution, similar to https://guide.meteor.com/data-loading.html#loading-from-rest

In my opinion, either option is better than calling a method or resubscribing every Ns.

@matt, the problem i’m having is that I have to replace all the data I have in the client then publish a new set of data since the data are based on a MYSQL query as my external source… If I don’t delete it then there will be duplicate data…

How should I do this?

To make sure that I understand, is the issue that, without clearing the client-side collection before publishing a new set of results, the collection will contain both the new and the old results?

If so, then using two (or more) client-side collections and a little indirection may solve the problem.

  1. Publish to one of those collections (possibly along with some metadata on each record - such as a publishedAt property indicating when the record was published) but do not access that collection directly from the UI. Instead…

  2. Sync the data in the collection being published to with the other collection and access that other collection directly from the UI. The sync logic can be setup on an interval or triggered via reactivity.