Publications and Subscriptions
Explore how Meteor.js manages real-time data transfer using publications and subscriptions. Understand creating server-side publications to send reactive MongoDB data to clients and subscribing on the client to keep data synced. Learn to handle user-based data access and manage reactivity efficiently in your Meteor applications.
We'll cover the following...
Meteor data transfer
Meteor is famous for its speed and reactivity of data, which is evident when a change is made and an application is updated instantly. This is as a result of its DDP.
The server publishes the data that’s subscribed to by the client. Meteor applications consist of two data stores, as illustrated in the diagram below:
Publication
A publication is a named API on the server and sends data to the client through DDP. Publications server-only function that accepts two arguments. The first argument is the name of the publication, which must be a unique string, while the second argument is a function that’s called on the server each time a client subscribes to that publication.
Assuming we have a Mongo collection in Meteor referred to as Students, we can create a publication that returns a subset of data to the client, like this:
//called only on the server
Meteor.publish("student.listBoys",function() {
return Students.find({sex: "male"})
});
The code snippets above create a publication called ...