Pusher Customer Client Events

I am writing a multiplayer chess game and use Pusher for the part of the websocket server.

In any case, if I have a list of users and I select any of them and challenge them, how can I send a call to only one user? I know that I would use a client event, for example:

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

But this event should have already been created, I think. So can I create this event dynamically after each member subscribes? as possible 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 that there is an overloaded method for trigger , for example:

 channel.trigger(eventName, data, memberID) 

But I do not see anything like it. Any ideas? Thanks.

+4
source share
3 answers

I ran into this problem in my application. Pusher does not provide methods to send events to a specific user at this time. I think the approach you mentioned will work for your situation. For my application, each user subscribed to a channel with a user ID as the channel identifier, then I could send messages to one user through this channel.

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

I talked about this approach with a team of pushers, and they assured me that there was no real overhead on the additional channels. The new Pusher() command is code that creates a new socket connection, so you don’t need to worry about additional sockets per channel or something like that. Hope this helps.

+8
source

I'm from Pusher. According to Braden, you can easily create a channel for each user. This is more efficient than the user ID in the event name, which means that you spam all useless messages.

This is an area we want to improve, so thank you for your feedback.

+3
source

If you can consider another service, Beaconpush has the ability to send messages to a specific user.

From your site:

POST /1.0.0/[API key]/users/[user]

+1
source

Source: https://habr.com/ru/post/1342357/


All Articles