raphadko raphadko - 1 year ago 69
Node.js Question

Meteor.js publish only to one user based on their id

I'm creating a simple match making system for a game in Meteor, my architecture is pretty simple: I have a server side collection

that inserts user ids while they are waiting.

When a user joins the list, it will check if there is another user on the list, if there is, it should confirm if both players are ready and then creates the game.

On plain
node.js with socket.io
this would be very simple, since each user is in it's own room I'd just broadcast the message to both user rooms and get them both to receive a Are you ready? confirmation.

I'm new to meteor, and I'm a bit lost, how could I send confirm just for the two users selected on the list and not for all the users that are currently waiting?

Answer Source

Easiest way I can think of is to just publish the collection of waiting users to each user:

  return Meteor.users.find({ waiting: true, _id: { $ne: this.userId }},{ fields: { waiting: 1 }});

And then subscribe to that on the client. If there is another user waiting then you will see it on the client.

You'll still have to deal with situations when there are many people waiting and picking partners.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download