1

I am building a multiplayer turn based game, the communication between clients and server is established with Pusher. I can send events to all clients using game channels. The problem is how do I send an event to a single client? Pusher has no documentation for it, only seemingly solution is to use authenticated channels. Is it viable to authenticate a dedicated channel for every client sending events to a single client, or is there a better solution?

user3995789
  • 3,452
  • 1
  • 19
  • 35
  • you can just send an ID along with the data, then check for the proper ID on the client event handler, and ignore if not related to the client. it would be expensive and complicated to use one channel per client. – dandavis Sep 18 '14 at 01:21
  • @dandavis, it would be expensive to send everyone unrelated data. socket.io has a simple way of sending to specific sockets, how do I do it with Pusher? – user3995789 Sep 18 '14 at 01:23
  • these services are designed for broadcast, so that they can scale beyond what socket.io can do easily on a single thread. there simply is no "single client" emit/send/broadcast feature, so you'll have to do something else. many channels is one option, client-side filtering is another. If you care more about bandwidth than latency, a third option presents itself: you send a change notice w/id to all, and the client with the right id performs an ajax call to fetch private data. that lets you keep your signal pipe uncluttered with heavy or private data, but does add a few dozen ms of latency... – dandavis Sep 18 '14 at 01:33
  • I have to send every player their cards, so that has to be authenticated, what is the best solution for that @dandavis – user3995789 Sep 18 '14 at 01:37
  • 1
    you need a client secret known to the server and never send over the socket. the "details" ajax call should deliver the eventID, clientID, and the client secret to the server in it's requiest, which if they match the server's expectations, then delivers the data back in the ajax response. – dandavis Sep 18 '14 at 01:39
  • in short, just use the real-time service to deliver notifications to the client that more stuff is available for them. you don't have to go all-in to use it for everything, but take advantage of what it does make simple. the end result is something like one of those little slips of paper the mailman brings to your door that says go down to the post office with your ID to pickup a package... does that make sense? – dandavis Sep 18 '14 at 06:45

3 Answers3

0

You touched on the best solution in your answer. You should be able to quite easily programmatically setup channels for each individual user and then just broadcast messages to them over those channels.

e.g. (this is a Ruby example but it should be clear what's happening)

user = SOME_USER_OBJECT
Pusher.trigger("card-data-#{user.id}", 'card-update', {data: {card_id: 1, status: 'used'})

or something like that. Obviously you'd then need to make sure that on the client side that the users are subscribing to the correct channels.

Obviously if you need the channels to be secure then, as you said, you can use authenticated channels - probably using private channels makes sense in your case.

If you have any more questions then you can reply here again and I'll take a look, or you can email support at support@pusher.com.

hamchapman
  • 2,901
  • 4
  • 22
  • 37
  • dandavis suggest it would be overhead, and I should use Ajax for single communication. Maybe a hybrid approach? Do you think is it viable to dedicate channels to millions of each user? – user3995789 Sep 18 '14 at 09:28
  • I think it's best to create a channel for each user because otherwise, as you alluded to, you'll be sending a lot of users messages that aren't intended for them and will therefore be essentially wasted. There's no difference in the overhead for either you or the client because you're just triggering to different channels and the clients are still only subscribed to one (or however many you need) channels - they just happen to be different for each user. That's what Pusher helps you with. You can code it that way and not have to worry about the scaling - that's our problem :) – hamchapman Sep 18 '14 at 09:31
0

Instead of creating an individual channel you can subscribe to an individual event for each client.

Jonas Tomanga
  • 1,069
  • 10
  • 19
0

PubNub Stream Filter

If you are using PubNub, you can either create a unique channel for each user and just publish the proper message to each of the channels or you can create a common channel for all of the users and use the Stream Filter feature so that each client only gets messages they want on that channel. This filtering is performed on the server side so the end user doesn't get unwanted messages that have to be ignored.

This is the simple high level steps for using Stream Filters

  1. When you init PubNub on the client side, create a filter using the meta parameter
  2. When you publish messages, add key/values to the meta parameter that will be used to filter messages on the PubNub Network before sending them to the subscribers (based on each individual subscriber's filter).

For full Stream Filter docs per SDK (that you mentioned via tags):

You could also use PubNub BLOCKS to route each message to the appropriate user channel (or however you map your channels to end users) the on before publish event handler. I won't go into the details of this since it is slightly more involved but feel free to ask for additional insights as necessary. To get started, you can review the full BLOCKS docs.

Community
  • 1
  • 1
Craig Conover
  • 4,710
  • 34
  • 59