Question sounds pretty dumb, but I couldn't find an explicit answer to my question. I know that subscribing to a publication makes the data available to the client.
So what I want to understand is when does the data get downloaded into Minimongo. Does the client download all the data in a publication as soon as he subscribes to it? Or (which would make more sense), does the client only download the data as soon as he starts querying for the data. My terminology might be of, apologise for that. But maybe some code makes it clearer. All of the below is code run on the client side.
Subscribing:
const eventSub = Meteor.subscribe('getEvents');
const loading = !eventSub.ready();
Querying :
const fin = {_id:someid};
const eventData = loading ? null : Events.find(fin).fetch()[0]
Pub/Sub in meteor (as well as pretty much all client-server communication) is done using a protocol called DDP, usually done over a web socket (if it is not supported, there are fallbacks).
When a client subscribes to a publication, it sends a message to the server requesting a subscription. This invokes the handler (the publication function that you define and supply to Meteor.publish
, in your case) which can return a Mongo cursor, and array of cursors or handle the lower-level details of publishing on its own.
In case the function returns a cursor, the server observes it and sends messages regarding the data as soon as it can. At first, all matching documents are sent as added
messages to the client, which are automatically translated into documents in MiniMongo.
Later changes are sent as by the server cursor observer when the server notices them.
ready
is another message sent by the server and it tells the client that the server has sent it whatever it had at that moment.
This means that the data is sent to the client immediately (or, at least, ASAP), but not synchronously, and not in a single message.
A reactive computation (using Tracker
) can be used to subscribe, get the subscription's ready state and query for data as needed, as the ready()
method of that object is "reactive".