Channel Basics
A channel is a communication pathway for passing data between devices with PubNub.
Channels are hosted on PubNub. You interact with channels using PubNub SDKs (Software Development Kit, SDK) or the REST API (Representational State Transfer, REST).
Pub/sub enables efficient, asynchronous message processing. Your app can deliver real-time updates while components work independently. For example, in chat, messaging, notifications, and analytics can each communicate through channels to keep the design modular and scalable.
We are fast
Devices listening on a channel typically receive messages in about 30 ms, enabling swift global communication.
Channel creation
You don’t create channels in advance. PubNub creates a channel the first time you publish to it.
Channel entity
PubNub SDKs provide local Channel
entities to help you work with channels. These objects hide channel-management details and support clean patterns, such as decoupling app components for maintainability.
For more information, refer to Entities.
Channel limits
There’s no limit on the number of channels or users per channel. A device can listen to thousands of channels over a single connection, which supports large-scale communication.
Channel types
Channels represent places where messages are sent and received. Use direct channels for one-to-one private messaging and group channels for many-to-many conversations. Broadcast and unicast channels fit one‑to‑many and many‑to‑one use cases.
Here are some useful channel configurations:
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.
- JavaScript
- Swift
- Objective-C
- Kotlin
- C#
- Python
1const PubNub = require('pubnub');
2
3// Initialize the PubNub client with your publish and subscribe keys
4const pubnub = new PubNub({
5 publishKey: 'yourPublishKey',
6 subscribeKey: 'yourSubscribeKey',
7 userId: 'user1-unique-id', // Use unique user IDs for authentication
8 authKey: 'user1AuthKey', // Use an authentication key for security
9 ssl: true // Enable SSL for secure communication
10});
11
12// Define the direct channel for one-to-one messaging between two specific users
13const directChannel = 'direct.user1.user2'; // Example channel name
14
15// Subscribe to the direct channel
show all 48 lines1import PubNub
2
3// Initialize the PubNub client with your publish and subscribe keys
4let config = PubNubConfiguration(
5 publishKey: "yourPublishKey",
6 subscribeKey: "yourSubscribeKey",
7 userId: "user1-unique-id" // Unique ID for the user
8)
9let pubnub = PubNub(configuration: config)
10
11// Define the direct channel for one-to-one messaging
12let directChannel = "direct.user1.user2"
13
14// Subscribe to the direct channel
15let subscription = pubnub.channel(directChannel).subscription()
show all 42 lines1#import <PubNub/PubNub.h>
2
3@interface AppDelegate () <PNEventsListener>
4@property (nonatomic, strong) PubNub *client;
5@end
6
7@implementation AppDelegate
8
9- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10
11 PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"yourPublishKey" subscribeKey:@"yourSubscribeKey"];
12
13 configuration.uuid = @"user1-unique-id"; // Unique ID for the user
14
15 self.client = [PubNub clientWithConfiguration:configuration];
show all 45 lines1import com.google.gson.JsonObject
2import com.pubnub.api.PubNub
3import com.pubnub.api.UserId
4import com.pubnub.api.models.consumer.pubsub.PNMessageResult
5import com.pubnub.api.v2.PNConfiguration
6import com.pubnub.api.v2.callbacks.EventListener
7
8
9fun main() {
10 val config = PNConfiguration.builder(UserId("user1-unique-id"), "yourSubscribeKey").apply {
11 publishKey = "yourPublishKey"
12 }
13 val pubnub = PubNub.create(config.build())
14 val directChannelName = "direct.user1.user2"
15
show all 44 lines1using PubnubApi;
2
3class Program
4{
5 static void Main(string[] args)
6 {
7 PNConfiguration pnConfiguration = new PNConfiguration(new UserId("user1-unique-id"))
8 {
9 SubscribeKey = "yourSubscribeKey",
10 PublishKey = "yourPublishKey"
11 };
12 Pubnub pubnub = new Pubnub(pnConfiguration);
13
14 string directChannelName = "direct.user1.user2";
15 Subscription subscription = pubnub.Channel(directChannelName).Subscription();
show all 56 lines1from pubnub.pnconfiguration import PNConfiguration
2from pubnub.pubnub import PubNub
3from pubnub.callbacks import SubscribeCallback
4
5# Configuration setup
6config = PNConfiguration()
7config.subscribe_key = 'yourSubscribeKey'
8config.publish_key = 'yourPublishKey'
9config.user_id = 'user1-unique-id'
10pubnub = PubNub(config)
11
12# Listener Setup
13class MySubscribeCallback(SubscribeCallback):
14 def message(self, pubnub, message):
15 print(f"Received message: {message.message}")
show all 33 linesGroup channels serve as a collective category for many-to-many in-app messaging among multiple users, forming a community communication system, such as a chat room for friends or family.
- JavaScript
- Swift
- Objective-C
- Kotlin
- C#
- Python
1const PubNub = require('pubnub');
2
3// Initialize the PubNub client with your publish and subscribe keys
4const pubnub = new PubNub({
5 publishKey: 'yourPublishKey',
6 subscribeKey: 'yourSubscribeKey',
7 userId: 'user1-unique-id', // Use unique user IDs for authentication
8 authKey: 'user1AuthKey', // Use an authentication key for security if necessary
9 ssl: true // Enable SSL for secure communication
10});
11
12// Define the group channel for in-app messaging
13const groupChannel = 'group.family'; // Example channel name for a family group
14
15// Subscribe to the group channel
show all 68 lines1import PubNub
2
3// Initialize the PubNub client with your publish and subscribe keys
4let config = PubNubConfiguration(
5 publishKey: "yourPublishKey",
6 subscribeKey: "yourSubscribeKey",
7 userId: "user1-unique-id" // Unique ID for the user
8)
9let pubnub = PubNub(configuration: config)
10
11// Define the group channel for many-to-many messaging
12let groupChannel = "group.family"
13
14// Subscribe to the group channel
15let subscription = pubnub.channel(groupChannel).subscription()
show all 42 lines1#import <PubNub/PubNub.h>
2
3@interface AppDelegate () <PNEventsListener>
4@property (nonatomic, strong) PubNub *client;
5@end
6
7@implementation AppDelegate
8
9- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10
11 PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"yourPublishKey" subscribeKey:@"yourSubscribeKey"];
12
13 configuration.uuid = @"user1-unique-id"; // Unique ID for the user
14
15 self.client = [PubNub clientWithConfiguration:configuration];
show all 43 lines1import com.google.gson.JsonObject
2import com.pubnub.api.PubNub
3import com.pubnub.api.UserId
4import com.pubnub.api.models.consumer.pubsub.PNMessageResult
5import com.pubnub.api.v2.PNConfiguration
6import com.pubnub.api.v2.callbacks.EventListener
7
8fun main() {
9 val config = PNConfiguration.builder(UserId("user1-unique-id"), "yourSubscribeKey").apply {
10 publishKey = "yourPublishKey"
11 }
12 val pubnub = PubNub.create(config.build())
13 val groupChannelName = "group.family"
14 // Create Subscription
15 val channel = pubnub.channel(groupChannelName)
show all 41 lines1using PubnubApi;
2using System;
3using System.Collections.Generic;
4
5class Program
6{
7 static void Main(string[] args)
8 {
9 PNConfiguration pnConfiguration = new PNConfiguration(new UserId("user1-unique-id"));
10 pnConfiguration.SubscribeKey = "yourSubscribeKey";
11 pnConfiguration.PublishKey = "yourPublishKey";
12 Pubnub pubnub = new Pubnub(pnConfiguration);
13
14 string groupChannelName = "group.family";
15 Subscription subscription = pubnub.Channel(groupChannelName).Subscription();
show all 57 lines1from pubnub.pnconfiguration import PNConfiguration
2from pubnub.pubnub import PubNub
3from pubnub.callbacks import SubscribeCallback
4
5# Configuration setup
6config = PNConfiguration()
7config.subscribe_key = 'yourSubscribeKey'
8config.publish_key = 'yourPublishKey'
9config.user_id = 'user1-unique-id'
10pubnub = PubNub(config)
11
12# Listener Setup
13class MySubscribeCallback(SubscribeCallback):
14 def message(self, pubnub, message):
15 print(f"Received message: {message.message}")
show all 33 linesBroadcast channels for announcements, polls, and other situations in which you want to broadcast messages in a one-to-many arrangement.
- JavaScript
- Swift
- Objective-C
- Kotlin
- C#
- Python
1const PubNub = require('pubnub');
2
3// Initialize the PubNub client with your publish and subscribe keys
4const pubnub = new PubNub({
5 publishKey: 'yourPublishKey',
6 subscribeKey: 'yourSubscribeKey',
7 userId: 'broadcaster-unique-id', // Use a unique ID for the broadcaster
8 ssl: true // Enable SSL for secure communication
9});
10
11// Define the broadcast channel for one-to-many communication
12const broadcastChannel = 'broadcast.announcements'; // Example channel for announcements
13
14// Subscribe to the broadcast channel
15const channel = pubnub.channel(broadcastChannel);
show all 51 lines1import PubNub
2
3// Initialize the PubNub client with your publish and subscribe keys
4let config = PubNubConfiguration(
5 publishKey: "yourPublishKey",
6 subscribeKey: "yourSubscribeKey",
7 userId: "broadcaster-unique-id" // Unique ID for the broadcaster
8)
9let pubnub = PubNub(configuration: config)
10
11// Define the broadcast channel for one-to-many communication
12let broadcastChannel = "broadcast.announcements"
13
14// Subscribe to the broadcast channel
15let subscription = pubnub.channel(broadcastChannel).subscription()
show all 42 lines1#import <PubNub/PubNub.h>
2
3@interface AppDelegate () <PNEventsListener>
4@property (nonatomic, strong) PubNub *client;
5@end
6
7@implementation AppDelegate
8
9- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10
11 PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"yourPublishKey" subscribeKey:@"yourSubscribeKey"];
12
13 configuration.uuid = @"broadcaster-unique-id"; // Unique ID for the broadcaster
14
15 self.client = [PubNub clientWithConfiguration:configuration];
show all 43 lines1import com.google.gson.JsonObject
2import com.pubnub.api.PubNub
3import com.pubnub.api.UserId
4import com.pubnub.api.models.consumer.pubsub.PNMessageResult
5import com.pubnub.api.v2.PNConfiguration
6import com.pubnub.api.v2.callbacks.EventListener
7
8fun main() {
9 val config = PNConfiguration.builder(UserId("broadcaster-unique-id"), "yourSubscribeKey").apply {
10 publishKey = "yourPublishKey"
11 }
12 val pubnub = PubNub.create(config.build())
13 val broadcastChannelName = "broadcast.announcements"
14 // Create Subscription
15 val channel = pubnub.channel(broadcastChannelName)
show all 40 lines1using PubnubApi;
2using System;
3using System.Collections.Generic;
4
5class Program
6{
7 static void Main(string[] args)
8 {
9 PNConfiguration pnConfiguration = new PNConfiguration(new UserId("broadcaster-unique-id"));
10 pnConfiguration.SubscribeKey = "yourSubscribeKey";
11 pnConfiguration.PublishKey = "yourPublishKey";
12 Pubnub pubnub = new Pubnub(pnConfiguration);
13
14 string broadcastChannelName = "broadcast.announcements";
15 Subscription subscription = pubnub.Channel(broadcastChannelName).Subscription();
show all 58 lines1from pubnub.pnconfiguration import PNConfiguration
2from pubnub.pubnub import PubNub
3from pubnub.callbacks import SubscribeCallback
4
5# Configuration setup
6config = PNConfiguration()
7config.subscribe_key = 'yourSubscribeKey'
8config.publish_key = 'yourPublishKey'
9config.user_id = 'broadcaster-unique-id'
10pubnub = PubNub(config)
11
12# Listener Setup
13class BroadcastSubscribeCallback(SubscribeCallback):
14 def message(self, pubnub, message):
15 print(f"Received broadcast message: {message.message}")
show all 33 linesUnicast channels for poll responses, sensor inputs, location data, and other situations in which you want to aggregate messages in a many-to-one arrangement.
- JavaScript
- Swift
- Objective-C
- Kotlin
- C#
- Python
1const PubNub = require('pubnub');
2
3// Initialize the PubNub client with your publish and subscribe keys
4const pubnub = new PubNub({
5 publishKey: 'yourPublishKey',
6 subscribeKey: 'yourSubscribeKey',
7 userId: 'listener-unique-id', // Unique ID for the device/server listening for inputs
8 ssl: true // Enable SSL for secure communication
9});
10
11// Define the unicast channel for aggregating messages
12const unicastChannel = 'unicast.dataCollector'; // Example channel for aggregating data
13
14// Subscribe to the unicast channel
15const channel = pubnub.channel(unicastChannel);
show all 60 lines1import PubNub
2
3// Initialize the PubNub client with your publish and subscribe keys
4let config = PubNubConfiguration(
5 publishKey: "yourPublishKey",
6 subscribeKey: "yourSubscribeKey",
7 userId: "listener-unique-id" // Unique ID for the listener
8)
9let pubnub = PubNub(configuration: config)
10
11// Define the unicast channel for aggregating messages
12let unicastChannel = "unicast.dataCollector"
13
14// Subscribe to the unicast channel
15let subscription = pubnub.channel(unicastChannel).subscription()
show all 43 lines1#import <PubNub/PubNub.h>
2
3@interface AppDelegate () <PNEventsListener>
4@property (nonatomic, strong) PubNub *client;
5@end
6
7@implementation AppDelegate
8
9- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10
11 PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"yourPublishKey" subscribeKey:@"yourSubscribeKey"];
12
13 configuration.uuid = @"listener-unique-id"; // Unique ID for the listener
14
15 self.client = [PubNub clientWithConfiguration:configuration];
show all 43 lines1import com.google.gson.JsonObject
2import com.pubnub.api.PubNub
3import com.pubnub.api.UserId
4import com.pubnub.api.models.consumer.pubsub.PNMessageResult
5import com.pubnub.api.v2.PNConfiguration
6import com.pubnub.api.v2.callbacks.EventListener
7
8fun main() {
9 val config = PNConfiguration.builder(UserId("listener-unique-id"), "yourSubscribeKey").apply {
10 publishKey = "yourPublishKey"
11 }
12 val pubnub = PubNub.create(config.build())
13 val unicastChannelName = "unicast.dataCollector"
14 // Create Subscription
15 val channel = pubnub.channel(unicastChannelName)
show all 58 lines1using PubnubApi;
2using System;
3using System.Collections.Generic;
4
5class Program
6{
7 static void Main(string[] args)
8 {
9 PNConfiguration pnConfiguration = new PNConfiguration(new UserId("listener-unique-id"));
10 pnConfiguration.SubscribeKey = "yourSubscribeKey";
11 pnConfiguration.PublishKey = "yourPublishKey";
12 Pubnub pubnub = new Pubnub(pnConfiguration);
13
14 string unicastChannelName = "unicast.dataCollector";
15 Subscription subscription = pubnub.Channel(unicastChannelName).Subscription();
show all 66 lines1from pubnub.pnconfiguration import PNConfiguration
2from pubnub.pubnub import PubNub
3from pubnub.callbacks import SubscribeCallback
4
5# Configuration setup
6config = PNConfiguration()
7config.subscribe_key = 'yourSubscribeKey'
8config.publish_key = 'yourPublishKey'
9config.user_id = 'listener-unique-id'
10pubnub = PubNub(config)
11
12# Listener Setup
13class UnicastSubscribeCallback(SubscribeCallback):
14 def message(self, pubnub, message):
15 print(f"Received data: {message.message}")
show all 40 linesChannel groups allow you to bundle thousands of channels into a group that can be identified by name. When you subscribe to a channel group, you receive data from all the channels the channel group contains.
Channel names
A channel name can be any alphanumeric string up to 92 UTF‑8 characters. Channel names are unique per PubNub key set. You can reuse the same name in a different key set, even within the same account. A key set is the namespace for your channels.
Invalid characters
, : * / \
and Unicode Null, whitespace, and non‑printable ASCII characters.
Valid characters
_ - = @ . ! $ # % & ^ ;
A period (.) is valid. It is also a special character for wildcard features. Use it strategically to enable wildcard channel subscribe and Function wildcard channel binding.
Channel name validator
Check if your channel name is valid using the channel name validator.
Naming conventions
When it comes to naming your channels, we recommend that you provide a prefix that identifies the purpose of the channel or the types of messages that will be sent over those types of channels.
Following that prefix with a .
character further enhances the usefulness of the channel name concerning the wildcard features with several PubNub features, like wildcard subscribe, wildcard channel function binding, and advanced Presence configuration.
For further details on channel naming recommendations, refer to Channel Naming Conventions.
Securing channels
Secure channels by controlling access with PubNub’s Access Manager.
While you are learning PubNub, you can keep Access Manager disabled so any client can send and receive on any channel. For production, enable access control to protect your channels.
For app‑wide protection, see Access Control in the Security section.