Build real-time applications with PubNub pub/sub messaging
Agent Success
Agent success rate when using this tile
94%
Improvement
Agent success rate improvement when using this tile compared to baseline
1.29x
Baseline
Agent success rate without this tile
73%
Channels are named pathways for routing messages in PubNub. They act as labels that connect publishers with subscribers.
MyChannel and mychannel are different| Character | Name |
|---|---|
, | Comma |
: | Colon |
* | Asterisk |
/ | Slash |
\ | Backslash |
| Space |
. | Period (reserved for wildcards) |
// Good channel names
'chat-room-123'
'user_notifications_alice'
'sports-scores-nba'
'iot-sensor-device42'
// Bad channel names (will cause errors)
'chat room' // space
'user:alice' // colon
'sports/nba' // slash
'alerts.*' // wildcard (use only for subscriptions)// Convention: Sort user IDs alphabetically for consistent channel name
function getDirectChannelName(userId1, userId2) {
const sorted = [userId1, userId2].sort();
return `dm-${sorted[0]}-${sorted[1]}`;
}
// Results in: 'dm-alice-bob' (always the same regardless of who initiates)// Room-based channels
'room-project-alpha'
'room-social-lounge'
'room-support-team'
// With namespace prefix
'chat-group-12345'// Personal notifications
`user-notifications-${userId}`
// User activity feed
`user-feed-${userId}`
// User presence channel
`user-status-${userId}`// Sports hierarchy
'sports.football.scores'
'sports.basketball.news'
'sports.baseball.highlights'
// IoT sensor hierarchy
'iot.building1.floor2.temp'
'iot.building1.floor2.humidity'
// Subscribe to all with wildcard
pubnub.subscribe({ channels: ['sports.*'] });// Device telemetry
`device-telemetry-${deviceId}`
// Device commands
`device-commands-${deviceId}`
// Fleet-wide broadcasts
'devices-all-updates'Requires: Stream Controller enabled in Admin Portal
*) must be at the end of the pattern.) is the hierarchy delimiter// One level
pubnub.subscribe({ channels: ['news.*'] });
// Matches: news.sports, news.weather, news.politics
// Two levels
pubnub.subscribe({ channels: ['iot.building1.*'] });
// Matches: iot.building1.temp, iot.building1.humidity// Wildcard not at end - INVALID
'news.*.sports'
// Too many levels - INVALID
'a.b.c.d.*'
// Wildcard at start - INVALID
'*.notifications'Requires: Stream Controller enabled in Admin Portal
Channel Groups allow subscribing to thousands of channels efficiently.
| Resource | Limit |
|---|---|
| Channels per group | 2,000 |
| Groups per client | 10 |
| Total channels via groups | 20,000 |
// Add channels to a group
await pubnub.channelGroups.addChannels({
channelGroup: 'user-alice-feeds',
channels: ['feed-news', 'feed-sports', 'feed-tech']
});
// Subscribe to the group
pubnub.subscribe({
channelGroups: ['user-alice-feeds']
});
// Remove channels from group
await pubnub.channelGroups.removeChannels({
channelGroup: 'user-alice-feeds',
channels: ['feed-tech']
});
// List channels in group
const result = await pubnub.channelGroups.listChannels({
channelGroup: 'user-alice-feeds'
});
console.log('Channels:', result.channels);
// Delete entire group
await pubnub.channelGroups.deleteChannelGroup({
channelGroup: 'user-alice-feeds'
});Subscribe to multiple named channels over a single connection.
// Subscribe to up to 30 channels (recommended max)
pubnub.subscribe({
channels: ['chat-room', 'notifications', 'presence-updates']
});| Scenario | Approach |
|---|---|
| < 30 channels | Multiplexing |
| 30-2,000 channels | Channel Groups |
| > 2,000 channels | Multiple Channel Groups |
| Hierarchical data | Wildcard Subscribe |
By default, any client can publish/subscribe to any channel. Use Access Manager to restrict:
// Server-side: Grant read access to specific channel
await pubnub.grant({
channels: ['private-room-123'],
authKeys: ['user-auth-token'],
read: true,
write: false,
ttl: 60 // minutes
});Each channel maintains a short-term buffer:
For longer storage, enable Message Persistence add-on.