1

I don't understand the concept of Meteor.subscribe. It is supposed to receive records from the server, and attach it to collections with the same name, right?

[subscribe] will queue incoming attributes until you declare the Meteor.Collection on the client with the matching collection name.

So, why the example in docs uses different names? What is the relation between allplayers and players?

Meteor.subscribe("allplayers");
...
// client queues incoming players records until ...
...
Players = new Meteor.Collection("players");
zVictor
  • 3,610
  • 3
  • 41
  • 56
  • 1
    Something else that might be confusing is Meteor's default behavior: enabling the **autopublish** smart package. Autopublish automatically publishes every collection to every client, *negating the need to manually publish or subscribe*. Though this is helpful while getting your project bootstrapped, the autopublish feature package is not recommended for production use. You will need to disable the autopublish package before your publications and subscriptions take effect. – Blackcoat Oct 12 '12 at 19:37
  • This detailed answer may help you [understand meteor subscriptions](http://stackoverflow.com/questions/19826804/understanding-meteor-publish-subscribe/21853298#21853298). – Dan Dascalescu Mar 31 '14 at 07:06

1 Answers1

3

There are two names:

  • The name of the collection ('players' in this case).
  • The name of the subscription ('allplayers' in this case).

A subscription is a way to get records into a client side collection. The name this collection that the records go into is decided (on the server side) by the use of this.set() in the relevant Meteor.publish function, but usually it is just name of the collection that is queried on the the server side[1].

Many subscriptions can deposit data into the same collection, so certainly the name of the subscription doesn't need to correspond to the name of the collection. In fact, it's probably only a good idea to have them be the same if you are doing a fairly straightforward single subscription to that collection.

[1] If you return a cursor (e.g. return players.find();) in Meteor.publish, it automatically wires up calls to this.set(name) for you, where name is inferred from the server side players collection.

Tom Coleman
  • 3,037
  • 18
  • 16
  • There don't seem to be `this.set()` (at least not anymore). The name of the collection is a parameter to `this.insert` and related methods. – Mitar Mar 20 '13 at 01:54
  • Got a question related to this here: http://stackoverflow.com/questions/27748100/in-meteor-how-can-i-query-only-the-records-of-a-given-subscription. Hoping to resolve the apparent loss of a records' association to the subscription it came from... – Dean Radcliffe Jan 02 '15 at 20:25