- Prevent publishing too much of data
You may have heard this one already. It’s one of the first performance optimizations a Meteor developer tends to discover. But given the performance cost from tracking a large amount of real-time data, it’s definitely worth a reminder.With Meteor, the server maintains a copy of every client’s version of the database. So, the more clients connected, the more copies it has to track. Now, having many clients means your app is being used and appreciated. So, you probably don’t want to reduce the number of clients. But you should reduce the potential performance hit by minimizing the amount of data needed by each client.We can do this via our Publish functions. The Publish functions make real-time data available for each client to use. So, if you can implement the Publish function to send only what is absolutely needed, the amount of data is reduced.To do this, you must first understand why every part of the data set is needed and how they will be used. Is the data associated with a certain user? If so, is only that user’s data needed? Will all the fields be required for display or processing on the client? Or, just a certain subset? Will all records be displayed at the same time? Or, will the user “page through” the data set? Asking questions like these will provide ideas on how to start narrowing your publication.Let’s say you’re developing a chat app. If only a list of authors participating in a certain channel are needed, then you can use field filtering in your publish function to send back only their names:
Meteor.publish(“channelParticipants”, function(channelId) {
return Messages.find({channelId: channelId}, {fields: {authorName: 1}});
});