Don't understand this.userId inside publish


Could someone please explain me how using this.userId inside publish actually works?
I am reading book “Your First Meteor Application” and there it says
"you can think of this process (publication) as transmitting some data from the server and into the ether"
This obviously isn’t true because when using this.userId you are transmitting specific data to specific users and not generally into the ether.
What exactly is this this.userId?
I know that it comes from accounts package but how could I create a variable like this on my own?
How such variables come from client to server?
Can I somehow add some other variables which I can then use during publishing?


When using Meteor Accounts, this.userId in a publication will be the id of a logged in client subscribing to that publication. This way you can ensure that a publication only publishes documents belonging to each user that subscribes.

Your publications can also include a parameter that the client can pass when it subscribes, but I wouldn’t use this for anything that is supposed to be private because it would be very easy for someone to subscribe passing any parameter they please.

The Meteor docs have some good examples:


There is no direct relationship between userId and the publishing process :

  • Publishing is about the server sending data to clients who request it. For any client request, it is up to the server to decide whether or not it will send the data (depending on any condition, such as the current date and time, the browser user agent, or whatever).

  • userId is the unique identifier of a logged-in user. The Meteor team did this very unfortunate choice of calling user what is actually a logged-in user (so there is no word left for a “not logged-in user”).

In a publish function, this.userId is the id of the requesting client if it is logged-in. If you want to, you can use this id to determine if you are willing to send data (it is a common use case, but not an obligation).


nkrisc said
"Your publications can also include a parameter that the client can pass when it subscribes,"
Is ‘this’ simply reference to an object containing parameters that client sent when it requested ti subscribe? And userId is just parameter that accounts package puts in that object?
Is this.userId part of accounts package or part of Meteor core functionality? If second can I somehow set this.userId to something else?
I am looking at
Are there any other more simpler and more focused tutorials on sending aditional data from client while subscribing?


Client sends data to the publish function through the publish function arguments. See the roomId argument here.


In the simplest sense, it would look something like this:

Meteor.publish('somePub', function(param){
	return SomeCollection.find({field: param})

Meteor.subscribe('somePub', paramValue)

I believe it is part of the Accounts package and I wouldn’t recommend trying to roll your own accounts/authentication system if that’s what you’re thinking. If you just want to send other data with a subscription, see the example above.


Thanks for the super simple example.
Just what I was looking for.

I am not looking to create my accounts implementation.
If this.userId is part of accounts package I would just like the know how did accounts package put userId into “this” so that I could put my stuff in it too.
“this” obviously isn’t parameter sent from client through subscribe(), it isn’t session property, it is not taken from MongoDB - so what the hell is it and how do you use it?

Links that are being given here are to complicated for me.
I need simpler examples like the one you provided.
If possible.


@ivoronline, all of Meteor.publish and Meteor.methods are endpoints for server calls. In the way Meteor uses its own DDP ptotocol worked via sockets, there is no way to identify userId with Session or Cookies like in classic REST API. So Meteor provides a this.userId property for every server call for you. If you would like to override userId you can use this.setUserId inside Meteor.methods .
When user is logged via Accouts package - the new generated user loginToken is stored in Meteor.users collection, and in a local storage on a client side. Every server call are signed with that loginToken and Meteor simply find a userId from Meteor.Users collection. If you want to logout one or all users you can simly clear all loginTokens from Meteor.Users collection


To access your own data, don’t worry about trying to piggy back on the userId. Just define your parameters in the publish callback and use them in that function. Then when you subscribe pass those parameters as arguments to the subscribe method. You can put them in an object literal if you want to just have one parameter.