I would like to run some client-independent regular tasks in the background of a Meteor app (like scraping some pages). So they should not be inside any client thread, but once they finish, I would like to update all clients with information. What is the best way to achieve this?
-
Please note that there is no such thing as a "client thread" in Meteor, contrary to more traditional solutions. Client requests are processed asynchronously, not multi-threaded. – Kyll Jul 31 '15 at 21:26
-
You are right. They are fibers, a cooperative multi-tasking. – Mitar Jul 31 '15 at 21:35
4 Answers
Run them on your server side code. If by regular you mean timed tasks every day or something:
You could use a cron job with Tom Coleman's cron package : https://github.com/tmeasday/meteor-cron.
You'll need to install the meteorite package manager first : npm install meteorite -g
and then install the cron package in your project dir mrt add cron-tick
Server js
var MyCron = new Cron();
// this job will happen every day (60 seconds * 60 * 24)
MyCron.addJob(60*60*24, function() {
//Scrape your stuff
//Update your collections
});
As soon as you run your update/insert/edit they will be pushed to all clients.

- 143,271
- 52
- 317
- 404

- 75,157
- 39
- 215
- 276
-
Is there a way to push something to clients which is not based on the collection (so not based on something in the database)? Like just information that something has been done? A notification? – Mitar Mar 15 '13 at 00:44
-
1Its a bit tricker than that, javascript in node will in a single thread, so this is where Fibers comes in. If you use a Meteor.call in the cron to a meteor.method with your task containing `this.unblock` the content in the method from `this.unblock` would run in a new fiber so as not to block other clients. Regarding what other stuff meteor can send, I'd recommend the counts by room collection: http://stackoverflow.com/questions/10565654/how-does-the-messages-count-example-in-meteor-docs-work a good place to start, it depends what exactly you want to send down. – Tarang Mar 15 '13 at 01:28
-
But in which fiber does cron tasks run if you do not use `this.unblock`? – Mitar Mar 15 '13 at 05:04
To do this in a way that allows arbitrary external processes update the Meteor clients, use the DDP protocol that's associated with Meteor. Your server processes can write to the DDP channel, and when they do your clients will update. Have a look at this post for an example and a use case, which may be similar to yours:
Using node ddp-client to insert into a meteor collection from Node
The protocol is fairly straight forward, and the post shows an example of a node.js process writing to a Mongo collection that updates the clients in real time.
-
-
-
An excellent solution. A couple things to call out: 1. To achieve periodic execution, set up a regular cron job which runs your program at whatever interval you'd like; 2. Your periodic program doesn't even have to be written in javascript, as long as the language has a DDP client library you're all set. – alanning Sep 02 '13 at 12:22
You could try calling a Meteor.setInterval
on the server (perhaps in Meteor.startup
). That should work, though it might not be as flexible as the cron solution.

- 21
- 1
-
1The idea is that I can have independent processes, even split them among multiple servers. So, really, heavy lifting on worker machines. – Mitar Apr 09 '13 at 22:39
Go to http://atmospherejs.com and search for cron
The best one I found is percolate:synced-cron
Installation
meteor add percolate:synced-cron
Basics
SyncedCron.add({
name: 'Crunch some important numbers for the marketing department',
schedule: function(parser) {
// parser is a later.parse object
return parser.text('every 2 hours');
},
job: function() {
var numbersCrunched = CrushSomeNumbers();
return numbersCrunched;
}
});
SyncedCron.start();
Advanced

- 4,085
- 7
- 41
- 53