Channel types and names

Channel is a mechanism by which messages are always published. You don't have to define channels in advance; the act of publishing a message creates the channel if it doesn't already exist.

PubNub supports an unlimited number of channels. A channel name can be any alphanumeric string up to 92 characters. There are a few invalid characters and some that are reserved for special features.

Refer to Channels section to learn more.

Channel Types

Channels represent any place where messages are sent and received. For example, a channel meant for 1-to-1 direct chat simply means that there are only two users using that channel. A group channel means there are two or more users using that channel.

Direct channels for one-to-one in-app messaging between two users. You can make these channels private to keep the messages secure between the users.

const PubNub = require('pubnub');

// Initialize the PubNub client with your publish and subscribe keys
const pubnub = new PubNub({
publishKey: 'yourPublishKey',
subscribeKey: 'yourSubscribeKey',
userId: 'user1-unique-id', // Use unique user IDs for authentication
authKey: 'user1AuthKey', // Use an authentication key for security
ssl: true // Enable SSL for secure communication
});

// Define the direct channel for one-to-one messaging between two specific users
const directChannel = 'direct.user1.user2'; // Example channel name

// Subscribe to the direct channel
show all 48 lines

Group channels for many-to-many in-app messaging between multiple users. For instance, a chat room for your family, or for a group of friends. You can make these channels made public to allow anyone to join, or make them private and only allow select users to join them.

const PubNub = require('pubnub');

// Initialize the PubNub client with your publish and subscribe keys
const pubnub = new PubNub({
publishKey: 'yourPublishKey',
subscribeKey: 'yourSubscribeKey',
userId: 'user1-unique-id', // Use unique user IDs for authentication
authKey: 'user1AuthKey', // Use an authentication key for security if necessary
ssl: true // Enable SSL for secure communication
});

// Define the group channel for in-app messaging
const groupChannel = 'group.family'; // Example channel name for a family group

// Subscribe to the group channel
show all 68 lines

Broadcast channels for announcements, polls, and other situations in which you want to broadcast messages in a one-to-many arrangement.

const PubNub = require('pubnub');

// Initialize the PubNub client with your publish and subscribe keys
const pubnub = new PubNub({
publishKey: 'yourPublishKey',
subscribeKey: 'yourSubscribeKey',
userId: 'broadcaster-unique-id', // Use a unique ID for the broadcaster
ssl: true // Enable SSL for secure communication
});

// Define the broadcast channel for one-to-many communication
const broadcastChannel = 'broadcast.announcements'; // Example channel for announcements

// Subscribe to the broadcast channel
const channel = pubnub.channel(broadcastChannel);
show all 51 lines

Unicast channels for poll responses, sensor inputs, location data, and other situations in which you want to aggregate messages in a many-to-one arrangement.

const PubNub = require('pubnub');

// Initialize the PubNub client with your publish and subscribe keys
const pubnub = new PubNub({
publishKey: 'yourPublishKey',
subscribeKey: 'yourSubscribeKey',
userId: 'listener-unique-id', // Unique ID for the device/server listening for inputs
ssl: true // Enable SSL for secure communication
});

// Define the unicast channel for aggregating messages
const unicastChannel = 'unicast.dataCollector'; // Example channel for aggregating data

// Subscribe to the unicast channel
const channel = pubnub.channel(unicastChannel);
show all 60 lines

You can secure your channels by controlling the access to them using Access Manager which is discussed in User Permissions. With Access Manager disabled, any client can freely send and receive messages on any channel. This is fine while you're learning to use PubNub but eventually, you'll need to secure your channels.

Channel Names

A channel name can be any alphanumeric string up to 92 UTF-8 characters. Channel names are unique per PubNub key, and you can have the same name in another key, even within the same PubNub account. In other words, a PubNub key is a namespace for your channels.

Invalid Characters

, : * / \ and Unicode Zero, whitespace, and non-printable ASCII characters.

Valid Characters

_ - = @ . ! $ # % & ^ ;

A period (.) is valid, however, it's a special character that is used for wildcard features and is encouraged to be used strategically to leverage wildcard channel subscribe and Function wildcard channel binding.

Last updated on