Meteor private messaging between users


Right now I have a working messaging system developed in Meteor where users can send private messages to each other.

The server looks like this:

// .. lot of code Meteor.publish("privateMessages", function () { return PMs.find({ to: this.userId }); }); PMs.allow({ insert: function(user, obj) { obj.from = user; obj.to = Meteor.users.findOne({ username: obj.to })._id; obj.read = false; obj.date = new Date(); return true; } }); // .. other code

When the user subscribes to privateMessages, he gets a mongo object that looks like this:

{ "to" : "LStjrAzn8rzWp9kbr", "subject" : "test", "message" : "This is a test", "read" : false, "date" : ISODate("2014-07-05T13:37:20.559Z"), "from" : "sXEre4w2y55SH8Rtv", "_id" : "XBmu6DWk4q9srdCC2" }

How can I change the object to return the username instead of the user id?


You need to do so in a way similar to how you changed username to _id. You can create a utility function:

var usernameById = function(_id) { var user = Meteor.users.findOne(_id); return user && user.username; };


If you don't want to poll minimongo for each message, just include username instead of _id inside your message object. Since username is unique, they will be enough.

If in your app you allow users to change username, it might be a good idea to <em>also</em> keep the _id for the record.

In one of larger apps I've been working with we kept user's _id in the model (to create links to profile etc.), as well as cached his profile.name (for display purposes).


I suggest adding the collection helpers package from atmosphere. Then create a helper for PMs called toUser that returns the appropriate user. Then you can get the name using message.user.name.


  • Find by id or username in mongo
  • Spring-MVC using a Converter to load object from path variable, but need to return 404 for unfound
  • Using publish-composite with Angular-Meteor
  • Finding a MongoDB document through a word in a field description in each product with Mongoskin
  • Android CalendarContract, deleting a recurring event causes all events to disappear on calendar?
  • FindAndModify, return array of Objects
  • Mongodb exception, “ MongoCursorException' with message '$ operator made object too large”
  • List comprehension with if conditional to get list of files of a specific type
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • Combining two different ActiveRecord collections into one
  • Using a canvas object in a thread to do simple animations - Java
  • Stop Bash Script if Hive Fails
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • MongoError: Incorrect arguments
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Django rest serializer Breaks when data exists
  • Illegal mix of collations for operation for date/time comparison
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Release, debug version and Authorization Google?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to disable jQuery.jplayer autoplay?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Getting error when using KSoap library to consume .NET web services
  • Hits per day in Google Big Query
  • How to stop GridView from loading again when I press back button?
  • LevelDB C iterator
  • Authorize attributes not working in MVC 4
  • Bitwise OR returns boolean when one of operands is nil
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF