问题
I'm learning pubnub and I read their documentation but I just can't find how to manage a multi room chat box.
By default, a channel can be listened by anyone. Subscribing to it and publishing on it is easy.
What I want is to have a main public room (so far so good) but anyone should also be able to talk privately to anyone else without the risk of being read by other users.
These dynamic rooms would be tabbed and the user should be able to go from one to another.
Another requirement would be that talking privately to someone doesn't kick you out of the other rooms you subscribed to (you can still be notified that a new message has been posted on another room while chatting)
What would be the best practice to achieve this?
- Would I use different channels (created dynamically)?
- Would I use one channel and filter the messages according to their status, pseudo-room id or protagonists?
- Would I use multiplexing (one socket only)?
I need to know the best way to achieve this as the documentation only describes basic scenarios with a single room and there's nothing about this on the internet.
Thank you.
PS: I know PubNub doesn't recommend more than 2 channels at a time (even though I have a hard time finding the explanation of this).
PPS: I'm using punbub with socket.io
回答1:
Socket.IO and PubNub Managing Private Rooms for Chat Services
You are asking for a way to create a Multiroom Chat Service, likely similar to IRC clients, where you are able to join and sit in on multiple chat rooms (on freenode.net for example). This is possible and will take some special tasks to get it right on your part.
You will start by opening Two Channels, one for the main chat room and one for your Private "ME" side chats. With this side Private "ME" channel, you will need to create a long and unpredictable session-id style channel name which typically looks like this:
YTQyOGFiNWEtNTZmZC00OGVhLTgxZjktMWE3ZmMyMzc3MTRhCg==
This is like a reference ID for the user. This ID can be used for Private 1-on-1 chats and the other room can be used for Group Chat. You will secure this Group chat using Access Control Management that we call PubNub Access Manager (PAM).
For additional Security Practices, you will need to review our security recommendations guides available here on PubNub Support for Security on our Help Desk and Knowledge Base.
Now that we have the private channels established, secure communication will be possible by sending and receiving chats via your private server (one that can provide authority) to allow messages to be relayed on a per user basis. You can learn how to do this by reading this section of the Sending Events from a Server to a Socket IO Client Documentation on PubNub.
The second channel will be for public chat for all rooms. For the Multi-tab support you will simply use the channel multiplexing feature of Socket IO on PubNub by adding new rooms via io.connect() method. Every time you open a new tab, you will open a new namespace via io.connect() of which you can have unlimited. Note however that you only should connect to no more than 2 PubNub channels at once (which you already noted in your question).
Here is the PubNub Socket IO method to subscribing to multiple feeds and categories:
Socket.IO Documentation
https://github.com/pubnub/pubnub-api/tree/493d7fc97fb683379fc78be3ca7ad9bc97eb4200/socket.io#restricting-yourself-to-a-namespace
Socket.IO Video on Vimeo
http://vimeo.com/34496366
Example Socket.IO Multiplexing Code
https://github.com/pubnub/pubnub-api/tree/493d7fc97fb683379fc78be3ca7ad9bc97eb4200/socket.io/multiplexing
As a quick conclusion, you will use secure methods to establish a private "ME" channel to send/receive messages on a per-users basis. And a public "Chat" channel that pushes all the public chat room data. Multiplexing will be important for the public chat.
回答2:
The answers above were correct in 2012, but a lot has changed since then. Private chat rooms can be enabled with PubNub Access Manager, which explicitly grants Publish/Subscribe access on specific channels.
The way this works is through access tokens (aka an "Auth Key"). The developer (you) creates an Auth Key (basically any string of characters) and passes it to PubNub Access Manager. You then set the rules for this Auth Key (i.e. which channels the Auth Key can publish and/or subscribe to).
This Auth Key is provided to any device that needs access to the channel, and used when the device subscribes or publishes to the channel.
Basic docs are available here: http://www.pubnub.com/docs/javascript/tutorial/access-manager.html
Grant 60 minute read/write privilege to channel "privateChat" to an auth_key:
pubnub.grant({
channel : 'privateChat',
auth_key : 'abxyz12-auth-key-987tuv',
read : true,
write : true,
ttl : 60
});
回答3:
To add to the previous answer, I'm not sure if this is just me hacking things but in order to make multi channel connections with the current socket-io library, you need to set io.connected = false; before passing in another channel config object.
For example:
var pubnub_setup = {
channel : 'public_channel',
publish_key : 'demo',
subscribe_key : 'demo'
};
var private_setup = {
channel : 'private_channel',
publish_key : 'demo',
subscribe_key : 'demo'
};
// Make your public socket connections
var publicSocket = io.connect( 'http://pubsub.pubnub.com/pub_socket', pubnub_setup );
var anotherPublicSocket = io.connect( 'http://pubsub.pubnub.com/another_pub_socket', pubnub_setup);
// Set false otherwise you keep getting back the previous created socket which is
// bound to the original channel 'public_channel'
io.connected = false;
var secretSocket = io.connect( 'http://pubsub.pubnub.com/secret_mulitplex_socket_namespace', private_setup );
Now you can keep creating new secret sockets on the private channel.
来源:https://stackoverflow.com/questions/10459123/pubnub-best-practice-how-to-manage-private-rooms