Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manage what data is synced to a Meteor client's cache?

Tags:

meteor

I modified the leaderboard example to use two collections:

Players = new Meteor.Collection("players");
Tasks = new Meteor.Collection("tasks");

The Players collection has the 6 documents defined in the example.

> db.players.count()
6

The Tasks collection has 48,000 documents.

> db.tasks.count()
48000

As soon as I open the browser, Node jumps to 100% CPU and the client can't see any of the tasks records.

Players.find().count()
6
Tasks.find().count()
0

I tried defining query criteria but that only works on the server and doesn't help on the client.

Players.find({name:"Claude Shannon"}).count();
1
Tasks.find({tid:"t36254"}).count();
0

I'm guessing that 48,000 documents is too much to sync. That's causing Node to peg at 100% CPU and the client to throw errors like this: http://i.imgur.com/zPcHO.png.

How do I prevent syncing everything and only retrieve specific documents from the collection?

like image 823
mb. Avatar asked Apr 27 '12 06:04

mb.


1 Answers

The autopublish of Meteor, which publishes all of your collections to the client, is very impressive and makes things work fast, but it's kind of like Rails scaffolding functionality - not very useful for real apps - it's for learning and prototyping.

By default, Meteor automatically publishes every document in your collection to each connected client. To turn this behavior off, remove the package:

$ meteor remove autopublish

Then, learn to use the manual publish and subscribe functions, which offers you the control you need: http://docs.meteor.com/#publishandsubscribe

like image 166
Mattias Petter Johansson Avatar answered Sep 30 '22 14:09

Mattias Petter Johansson