sigmaxf
sigmaxf

Reputation: 8492

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 waitingList 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?

Upvotes: 0

Views: 59

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20226

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

Meteor.publish('otherWaitingUsers',()={
  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.

Upvotes: 1

Related Questions