PubNub best practice: How to manage private rooms?

后端 未结 2 1439
被撕碎了的回忆
被撕碎了的回忆 2021-02-11 02:54

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

相关标签:
2条回答
  • 2021-02-11 03:11

    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
     });
    
    0 讨论(0)
  • 2021-02-11 03:19

    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.

    0 讨论(0)
提交回复
热议问题