Channel Basics
A channel is a fundamental communication pathway that facilitates the passing of data between devices within the PubNub service.
Channels are hosted on PubNub servers, and you interact with them using PubNub SDKs or directly via the REST API.
This pub/sub model allows for seamless and efficient asynchronous processing of messages, supporting both real-time updates and independent operation of different components within your application. For instance, in a real-time chat application, the messaging, notification, and analytics components can function independently and communicate through channels, enforcing modularity and scalability.
We are fast
Any device listening to a channel receives all messages in about 30ms regardless of their physical location in the world, ensuring swift communication across the globe.
Channel creation
You don't have to explicitly define or create channels on the server in advance. The channel is automatically created when you publish a message to a particular channel for the first time.
Channel entity
PubNub SDKs use local representations of channels called Channel
entitiesEntity
A subscribable object within a PubNub SDK that allows you to perform context-specific operations.
For more information, refer to Entities.
Channel limits
There is no limit to the number of channels or the number of users in a channel you can have. A device can listen to thousands of channels simultaneously through a single open connection, making PubNub ideal for scalable group communication.
Channel types
Channels can represent any place where messages are sent and received, supporting various communication models like direct channels for one-to-one private messaging or group channels for many-to-many interaction scenarios. Broadcast and unicast channels provide additional flexibility for specific 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
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 linesimport PubNub
// Initialize the PubNub client with your publish and subscribe keys
let config = PubNubConfiguration(
publishKey: "yourPublishKey",
subscribeKey: "yourSubscribeKey",
userId: "user1-unique-id" // Unique ID for the user
)
let pubnub = PubNub(configuration: config)
// Define the direct channel for one-to-one messaging
let directChannel = "direct.user1.user2"
// Subscribe to the direct channel
let subscription = pubnub.channel(directChannel).subscription()
show all 42 lines#import <PubNub/PubNub.h>
@interface AppDelegate () <PNEventsListener>
@property (nonatomic, strong) PubNub *client;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"yourPublishKey" subscribeKey:@"yourSubscribeKey"];
configuration.uuid = @"user1-unique-id"; // Unique ID for the user
self.client = [PubNub clientWithConfiguration:configuration];
show all 45 linesimport com.google.gson.JsonObject
import com.pubnub.api.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.models.consumer.pubsub.PNMessageResult
import com.pubnub.api.v2.PNConfiguration
import com.pubnub.api.v2.callbacks.EventListener
fun main() {
val config = PNConfiguration.builder(UserId("user1-unique-id"), "yourSubscribeKey").apply {
publishKey = "yourPublishKey"
}
val pubnub = PubNub.create(config.build())
val directChannelName = "direct.user1.user2"
show all 44 linesusing PubnubApi;
class Program
{
static void Main(string[] args)
{
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("user1-unique-id"))
{
SubscribeKey = "yourSubscribeKey",
PublishKey = "yourPublishKey"
};
Pubnub pubnub = new Pubnub(pnConfiguration);
string directChannelName = "direct.user1.user2";
Subscription subscription = pubnub.Channel(directChannelName).Subscription();
show all 56 linesfrom pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.callbacks import SubscribeCallback
# Configuration setup
config = PNConfiguration()
config.subscribe_key = 'yourSubscribeKey'
config.publish_key = 'yourPublishKey'
config.user_id = 'user1-unique-id'
pubnub = PubNub(config)
# Listener Setup
class MySubscribeCallback(SubscribeCallback):
def message(self, pubnub, message):
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
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 linesimport PubNub
// Initialize the PubNub client with your publish and subscribe keys
let config = PubNubConfiguration(
publishKey: "yourPublishKey",
subscribeKey: "yourSubscribeKey",
userId: "user1-unique-id" // Unique ID for the user
)
let pubnub = PubNub(configuration: config)
// Define the group channel for many-to-many messaging
let groupChannel = "group.family"
// Subscribe to the group channel
let subscription = pubnub.channel(groupChannel).subscription()
show all 42 lines#import <PubNub/PubNub.h>
@interface AppDelegate () <PNEventsListener>
@property (nonatomic, strong) PubNub *client;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"yourPublishKey" subscribeKey:@"yourSubscribeKey"];
configuration.uuid = @"user1-unique-id"; // Unique ID for the user
self.client = [PubNub clientWithConfiguration:configuration];
show all 43 linesimport com.google.gson.JsonObject
import com.pubnub.api.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.models.consumer.pubsub.PNMessageResult
import com.pubnub.api.v2.PNConfiguration
import com.pubnub.api.v2.callbacks.EventListener
fun main() {
val config = PNConfiguration.builder(UserId("user1-unique-id"), "yourSubscribeKey").apply {
publishKey = "yourPublishKey"
}
val pubnub = PubNub.create(config.build())
val groupChannelName = "group.family"
// Create Subscription
val channel = pubnub.channel(groupChannelName)
show all 41 linesusing PubnubApi;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("user1-unique-id"));
pnConfiguration.SubscribeKey = "yourSubscribeKey";
pnConfiguration.PublishKey = "yourPublishKey";
Pubnub pubnub = new Pubnub(pnConfiguration);
string groupChannelName = "group.family";
Subscription subscription = pubnub.Channel(groupChannelName).Subscription();
show all 57 linesfrom pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.callbacks import SubscribeCallback
# Configuration setup
config = PNConfiguration()
config.subscribe_key = 'yourSubscribeKey'
config.publish_key = 'yourPublishKey'
config.user_id = 'user1-unique-id'
pubnub = PubNub(config)
# Listener Setup
class MySubscribeCallback(SubscribeCallback):
def message(self, pubnub, message):
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
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 linesimport PubNub
// Initialize the PubNub client with your publish and subscribe keys
let config = PubNubConfiguration(
publishKey: "yourPublishKey",
subscribeKey: "yourSubscribeKey",
userId: "broadcaster-unique-id" // Unique ID for the broadcaster
)
let pubnub = PubNub(configuration: config)
// Define the broadcast channel for one-to-many communication
let broadcastChannel = "broadcast.announcements"
// Subscribe to the broadcast channel
let subscription = pubnub.channel(broadcastChannel).subscription()
show all 43 lines#import <PubNub/PubNub.h>
@interface AppDelegate () <PNEventsListener>
@property (nonatomic, strong) PubNub *client;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"yourPublishKey" subscribeKey:@"yourSubscribeKey"];
configuration.uuid = @"broadcaster-unique-id"; // Unique ID for the broadcaster
self.client = [PubNub clientWithConfiguration:configuration];
show all 43 linesimport com.google.gson.JsonObject
import com.pubnub.api.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.models.consumer.pubsub.PNMessageResult
import com.pubnub.api.v2.PNConfiguration
import com.pubnub.api.v2.callbacks.EventListener
fun main() {
val config = PNConfiguration.builder(UserId("broadcaster-unique-id"), "yourSubscribeKey").apply {
publishKey = "yourPublishKey"
}
val pubnub = PubNub.create(config.build())
val broadcastChannelName = "broadcast.announcements"
// Create Subscription
val channel = pubnub.channel(broadcastChannelName)
show all 40 linesusing PubnubApi;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("broadcaster-unique-id"));
pnConfiguration.SubscribeKey = "yourSubscribeKey";
pnConfiguration.PublishKey = "yourPublishKey";
Pubnub pubnub = new Pubnub(pnConfiguration);
string broadcastChannelName = "broadcast.announcements";
Subscription subscription = pubnub.Channel(broadcastChannelName).Subscription();
show all 58 linesfrom pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.callbacks import SubscribeCallback
# Configuration setup
config = PNConfiguration()
config.subscribe_key = 'yourSubscribeKey'
config.publish_key = 'yourPublishKey'
config.user_id = 'broadcaster-unique-id'
pubnub = PubNub(config)
# Listener Setup
class BroadcastSubscribeCallback(SubscribeCallback):
def message(self, pubnub, message):
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
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 linesimport PubNub
// Initialize the PubNub client with your publish and subscribe keys
let config = PubNubConfiguration(
publishKey: "yourPublishKey",
subscribeKey: "yourSubscribeKey",
userId: "listener-unique-id" // Unique ID for the listener
)
let pubnub = PubNub(configuration: config)
// Define the unicast channel for aggregating messages
let unicastChannel = "unicast.dataCollector"
// Subscribe to the unicast channel
let subscription = pubnub.channel(unicastChannel).subscription()
show all 43 lines#import <PubNub/PubNub.h>
@interface AppDelegate () <PNEventsListener>
@property (nonatomic, strong) PubNub *client;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
PNConfiguration *configuration = [PNConfiguration configurationWithPublishKey:@"yourPublishKey" subscribeKey:@"yourSubscribeKey"];
configuration.uuid = @"listener-unique-id"; // Unique ID for the listener
self.client = [PubNub clientWithConfiguration:configuration];
show all 43 linesimport com.google.gson.JsonObject
import com.pubnub.api.PubNub
import com.pubnub.api.UserId
import com.pubnub.api.models.consumer.pubsub.PNMessageResult
import com.pubnub.api.v2.PNConfiguration
import com.pubnub.api.v2.callbacks.EventListener
fun main() {
val config = PNConfiguration.builder(UserId("listener-unique-id"), "yourSubscribeKey").apply {
publishKey = "yourPublishKey"
}
val pubnub = PubNub.create(config.build())
val unicastChannelName = "unicast.dataCollector"
// Create Subscription
val channel = pubnub.channel(unicastChannelName)
show all 58 linesusing PubnubApi;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("listener-unique-id"));
pnConfiguration.SubscribeKey = "yourSubscribeKey";
pnConfiguration.PublishKey = "yourPublishKey";
Pubnub pubnub = new Pubnub(pnConfiguration);
string unicastChannelName = "unicast.dataCollector";
Subscription subscription = pubnub.Channel(unicastChannelName).Subscription();
show all 66 linesfrom pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.callbacks import SubscribeCallback
# Configuration setup
config = PNConfiguration()
config.subscribe_key = 'yourSubscribeKey'
config.publish_key = 'yourPublishKey'
config.user_id = 'listener-unique-id'
pubnub = PubNub(config)
# Listener Setup
class UnicastSubscribeCallback(SubscribeCallback):
def message(self, pubnub, message):
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, and you can have the same name in another key, even within the same PubNub account. In other words, a PubNub key set 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's used for wildcard features and is encouraged to be used strategically to leverage wildcard channel subscribe and Function wildcard channel binding.
Channel name validator
Check if your channel name is valid using our 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
You can secure your channels by controlling the access to them using PubNub's Access Manager.
With Access Manager disabled, any client can 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.
For comprehensive protection across your app, refer to our documentation on Access Control in the Security section.