Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pusher App Client Events

I'm writing a multiplayer chess game, and using Pusher for the websocket server part.

Anyways, if I have a list of users, and I select any one of them and challenge them, how do I send challenge to just that one user? I know I would use the client event like:

channel.trigger("client-challenge_member1", {some : "data"});

But this event would have to have already been created I think. So do I create this event dynamically after each member subscribes? as possibly in:

channel.bind("pusher:subscribed_completed", function(member) // not sure of correct syntax but...
{
   channel.bind("client-challenge_" + member.memberID, function(data)
   {
      alert(data.Name + " is challenging you.");
   });
});

I would think there'd be a overloaded method for trigger, like:

channel.trigger(eventName, data, memberID)

But I cannot see anything like this. Any ideas? Thanks.

like image 512
David Avatar asked Dec 17 '22 16:12

David


1 Answers

I ran into this problem on my application. At this time Pusher does not provide methods for sending events to a specific user. I think the approach that you mentioned would work for your situation. For my application I had each user subscribe to a channel with their user id as the channel id, then I could send messages to a single user through that channel.

client = new Pusher(PUSHER_API_KEY);
channel = client.subscribe(user_id);
channel.bind('my_event',function(data){ 
  //Do stuff
}); 

I talked this approach over with the pusher team and they assured me there was no real overhead in having the extra channels. The new Pusher() command is the code that creates a new socket connection so you don't have to worry about extra sockets per channel or anything like that. Hope this helps.

like image 139
Braden Becker Avatar answered Feb 06 '23 01:02

Braden Becker