Possible to access subscription data without declaring collection?

From the Meteor docs
http://docs.meteor.com/#/full/meteor_subscribe

// okay to subscribe (and possibly receive data) before declaring
// the client collection that will hold it.

If the client calls Meteor.subscribe(‘players’), but there is no corresponding collection on the client , e.g. this has not been done on the client or inside a /both directory :

var players = new Mongo.Collection(‘players’);

where does the data go? Is there an accessible object or API the client can use to access this data, and/or ddp messages for the subscription ? Or, is the only way via a Mongo.Collection object ?

Docs also say :

Meteor will queue incoming records until you declare the
Mongo.Collection on the client with the matching
collection name.

Does that mean the subscription data is not sent to the client until the client Mongo Collection is declared ?

Subscription is based entirely on how you publish on the server.

Say you have this on both/collection:
// you have to remove ‘var’ keyword to make your collection global

Players = new Mongo.Collection('players');

You have to create a publication on the server:

Meteor.publish('nameofpublication', function() {
  return Players.find(); // you collection global variable
});

on the client:

Meteor.subscribe('nameofpublication');

I’m not sure if this answer your question.

Or you want a client only collection?

Like this:
// none persistent collection on client/
Players = new Mongo.Collection(null);

thanks @ajaxsoap

My question is more about the behavior of a subscription/publication when there is no corresponding client-side collection. Let me see if I can clarify a little better.

on server only :
Players = new Mongo.Collection(‘players’);
Meteor.publish(‘players’);

on client only :
Meteor.subscribe(‘players’);
no client collection which corresponds to this subscription/publication

Are these queued records only on the server, or are they sent to the client ? Can they be accessed in some way ?

I did a test and it looks like the DDP messages do get sent down when you call Meteor.subscribe(‘CollectionName’) whether or not you have declared a collection on the client.

Yeah, I think “Meteor will queue incoming records” means that the data you subscribe will be loaded to local.

what’s interesting is if you subscribe, then later declare the collection on the client, the collection will be populated with the queued records. This means there is some central store on the client independent of Mongo.Collection instances, I wonder what it is?

What is your use case?

From my previous comment, subscription is based on the publication you created on the server or a client collection.

Players = new Mongo.Collection(null) // none persistent collection

You cannot use collection name on the subcription, Meteor.subscribe('collectionName'), it should be the name of the publication.

Meteor.publish('name-of-publication', function() {
  return Players.find(); // you collection global variable
});

on the client you declare this to access the data from the collection you publish.
Meteor.subscribe('name-of-publication');

Meteor will queue incoming records until you declare the Mongo.Collection on the client with the matching collection name.

This means when you declare this on shared folder (both client and server),

Players = new Mongo.Collection('players');

Does this makes sense?

@ajaxsoap thanks. Use case is to try Redux without using minimongo. I think I can use a DDP client for that, I doubt the ‘headless collection’ client queue thing would be much good for my use case, but who knows, maybe it is. For now it’s got me just generally interested.

yeah, that makes sense, you may track this thread for more redux related discussions.

Here are some of the redux related threads:

Hope this helps you on understanding redux in Meteor. :+1:

1 Like