0

Just how Meteor.user() method is available on Client & Server for the "current user" I would love to reproduce this kind of functionality for different custom collections. For example, my app uses a "clouds" collection as a type of room for a group of users to be in. Obviously there are various cloud instances and I don't always want to be passing the cloudId into every single meteor method. Ideally I could have like a Meteor.cloud() function that would give me the current cloud on the client and server.

My thoughts on approaching this:

  1. What I have been doing thus far is piggy-backing off of Meteor.user() by storing a currentCloudId property inside the user profile and setting that on a route beforeAction. However this limits the user to only being in 1 cloud at a time.

  2. Using the Meteor.connection ID somehow to keep a map of connectionIds to cloudIds. This would work great in theory....however it seems that Meteor connection IDs cannot be heavily relied on as they might change during reconnects or other random scenarios. Also you would have to then "manange" that collection of "cloudConnections" and remove old stale ones and such.

  3. Im using Iron Router....and if it were possible to get the current route data on the server that would also solve my problem but I am not sure how to access that on the server?

--- Basically I would love for a simple straight forward way to mimic Meteor.user() behavior for other collections.

Thanks for all your help :)

3
  • What is each cloud ID linked to? Does a user always have a specific cloud ID? You can store an array of cloud IDs inside a user's document and then just use that as reference when you're subscribing to stuff in the router. Jun 24, 2015 at 4:02
  • A cloud has many users....and a user has many clouds. On the client inside of a cloud its easy to tell the cloudId because your subscribing to just that one cloud you are in for that specific meteor connection. But on the server just having an array of cloudIds...you wouldnt know which cloudId belonged to which connection if the user was in more than one cloud so you wouldnt know which cloud to update on the backend when a method is called from the front end. At the very least you would need to do a map of Meteor connectionIds to cloudIds for each user (maybe its own collection) like in #2
    – mccormjt
    Jun 24, 2015 at 4:39
  • If the client calls a Meteor Method simply pass the ID of the cloud as an argument into the Meteor Method. Then the server will know which ID / cloud document you're talking about. EDIT sorry, just read that this is exactly what you don't want to do. Jun 24, 2015 at 4:43

1 Answer 1

0

You can just create a function inside /lib that looks something like this:

getUserClouds = function getUserClouds () {
  return Clouds.find({ $elemMatch: { $eq: Meteor.userId() } })
}

This will work both on the client and on the server. But it will always return a Cursor pointing to 0 docs. So you'll need a publication:

Meteor.publish('userClouds', function () {
  return Clouds.find({ $elemMatch: { $eq: this.userId } })
})
1
  • I dont understand how this solves the problem. So are you saying that I should create an array of userIds on a cloud document for all the users inside of that specific cloud? From your setup....this looks like it will just give me a cursor of all the clouds that user has when on the server. But then there is no way of figuring out which of those clouds is the one being dealt with for that specific client connection (because they could be in more than 1)
    – mccormjt
    Jun 24, 2015 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.