How to send events to a single client with pusher pubnub socketio - javascript

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?

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.

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

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
When you init PubNub on the client side, create a filter using the meta parameter
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):
PubNub JavaScript SDK Stream Filter
PubNub Node SDK Stream Filter
PubNub Ruby SDK Stream Filter
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.

Related

Get List of users in Agora.io voice discussion room who do not publish stream

It is possible to get list of streamID by
client = AgoraRTC.createClient({mode, codec});
client.on('stream-subscribed'
But it is available only when the user publish stream
client.publish(localStream
[[[ Question ]]]
I want to see list of users who do not publish the stream but join in the room by
client.join(null, 'room-name', 'user-id');
As the user can hear the stream when that user joined the room without publishing own localStream.
Therefore I want to show all the audiences even if they do not publish the stream.
For the purpose to save bandwidth,
it is better not to publish localStream, and just subscribe voice.
Therefore I want to recognize all users even if they do not publish stream
Add a signaling System to the existing code so that a signal is sent to all user when a user joins the system. If it needs to be more perfectly implemented u can track user streams and do the listing.
You should use the peer-online event (Occurs when a remote user or host joins the channel.)
client.on('peer-online', function(evt) {
console.log('peer-online', evt.uid);
});
See full list of on events within Agora.io's Docs:
https://docs.agora.io/en/Video/API%20Reference/web/interfaces/agorartc.client.html#on
Update
per the comments below, this method only works in communication mode where all users have the broadcast role. In live mode there isn't a way to get the users using the Agora RTC SDK, but you can use the Agora RTM SDK to count the users in a channel.
The Agora RTM SDK allows you to create a data channel that each user joins along with the Live Video Broadcast. Agora RTM allows you to check the number of users in an RTM Channel before joining or after joining the RTM channel, you can use the MemberJoined and MemberLeft Channel Events to listen for new users.
Pinging all the available streams periodically might help in understanding when someone has entered the room but not streaming.

NodeJS connect two user among many of loggedin?

i'm experimenting node.js and made a cool chat application followed by this article using socet.io!
Then i have added a authentication system using Passport.Js into my test site.
How i would do this: among many users, user X want to send some kinds of notification to user Y not to other users!!
I am using the emit() function with a custom event called notifybuzz
but when a user send a notification it goes to all accounts!!
This article mentions that you will need to use the form socket.broadcast.emit('hi'); to send to limited recipients. the challenge will be to keep track of your socket connections, and relate user information to them. Once that's done, you'll know exactly which socket to send your message over.
Best of luck!

Pusher trigger an event across all subscribers on click

I am working with a Backbone front end, and we are currently making it realtime with the use of pusher. Most of the events are triggered from our API and then we listen to them on channels in our backbone front end.
However there is situation where we want to update a view for all subscribers to a channel on a button click, there is no server interaction here and we would like to keep is this way.
I have read the documentation for pusher, and it sounds like what we want is a client event, but from what I have read it sounds like this needs to be authenticated request...however by the virtue that the user is using the application means they have been authenticated.
Is there a way to broadcast an event from a client and not need a private channel or to authenticate?
There is not. If there were then anyone would be able to connect to Pusher independently of your application and publish arbitrary client events to your users.
Without the authentication provided by a private channel, it's not necessary to be using your application to join your channels. All that is required is your App ID, which is the equivalent to a username - that is to say, it should be considered public knowledge.
The same goes for broadcasting to public channels from your server. They really are public, if you want to restrict the audience to authenticated users of your app, you should use private channels.

Pubnub Presence feature in Python (GAE)

PubNub, its really awesome for real-time communication. as per as documention given by pubnub team, am done with subscribe and publish instance, its working fine.
Now am wondering how get a particular user already/presence with PubNub channels in Python(GAE) apps, but I didn't find a complete guide to how to implement this feature in both server and client side.
NOTE: am using here Python Google App Engine & Javascript.
PubNub presence is a way to follow up on joins/leaves in a channel, what you need is the here_now feature if I understand correctly,
Taken from the Pyton lib is (https://github.com/pubnub/pubnub-api/tree/master/python/)
here_now = pubnub.here_now({
'channel' : 'hello_world',
})
print(here_now['occupancy'])
print(here_now['uuids'])
And now you can iterate on whoever is in the channel right now,
this can't be done in the javascript though so you'd have to have perhaps another channel for each of your real channels that you can send the information from the server-side to the client side and then update something accordingly.

how to publish and subscribe channels dynamically ?

//Server setting....
//Store using Redis ..
this is the part of the code, now i have to send the stored data to other clients connected,the case is 'n' no.of application with 'n' no.of clients, so i have to create channels here dynamically. am asking u to help me by giving ideas for creating dynamic channel name for pub/sub.
This is not something Redis handles. You'll need to protect access to Redis pub/sub through an application layer. If you want to allow people to connect directly to Redis and still protect channels per user, I believe you're out of luck.

Categories