2

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

Baylock
  • 1,246
  • 4
  • 25
  • 49

3 Answers3

10

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.

Stephen Blum
  • 6,498
  • 2
  • 34
  • 46
  • 2
    Thanks. I'm flattered to get an answer from the founder of PubNub himself! I read these references but, as a beginner, I have to admit there are some gaps I'm struggling with. The private channels are easy to get as a concept but there's no example showing how to implement them from A to Z. That's not a complain as I'm sure it makes sense for any educated developer but for me, it's quite an ordeal. I just figured out it's not the channel I need to declare in order to communicate with the target user but the 1st argument of the "socket.emit" method. Simple enough but I haven't read it anywhere. – Baylock May 08 '12 at 01:42
  • 1
    @Pubnub. Steven, first you are a pimp! Watched your vimeo vids. You guys should do a bunch more. What I would really love to see is a real chat application built with pubnub. Like a full Olark clone. That would be a complex enough project that could include multiplexing and give us a real in depth view of what can be built. Not to mention a good reason to subscribe to pubnub. Also, can you please add ASP (classic) samples, and lastly XMPP to pubnub if possible. I dont know if possible but it would be sick to have a jabber client connect to pubnub. – Frank Jun 07 '12 at 05:57
  • 1
    Sorry but there are some broken links to your API. – Dunc Sep 16 '14 at 10:44
  • Hi Dunc, thank you for details. I will have this updated shortly. – Stephen Blum Sep 18 '14 at 03:53
  • 404 links everywhere – narek Feb 09 '17 at 10:35
  • updating the links. – Stephen Blum Feb 09 '17 at 17:24
  • finished! all links have been restored. – Stephen Blum Feb 09 '17 at 17:42
6

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
 });
Todd Greene
  • 111
  • 1
  • 2
0

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.

JCugno
  • 1