Search code examples
privatesocketchannelmultiplexingpubnub

PubNub best practice: How to manage private rooms?


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


Solution

  • 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.