Appearance
Public, Private & Presence Channels
Channels are the core of the Pub/Sub system. They allow one-to-many and many-to-many real-time communication between WebSocket clients.
Channel Types
The channel_type enum determines who can subscribe to a channel.
cpp
enum class channel_type : uint8_t {
public_channel, // Any client can subscribe (no JWT required)
private_channel, // Requires JWT with a grant for this channel
presence_channel // Like private, plus shows connected members
};| Type | Naming Convention | JWT Required | Members Visible |
|---|---|---|---|
| Public | Any name | No | No |
| Private | private-* | Yes (read grant) | No |
| Presence | presence-* | Yes (read grant) | Yes |
The Channel Object
Channels are managed by clients::registry. Each channel tracks its name, type, and usage metrics.
cpp
auto& clients = app.get_state()->of_clients();
// Get or create a channel
auto channel = clients.get_or_create_channel("room-general");
// channel->get_id() — UUID of the channel
// channel->get_name() — "room-general"
// channel->get_type() — public|private|presenceChannel Metrics
Each channel automatically tracks publish, subscribe, and unsubscribe counts:
cpp
channel->get_publish_count(); // Total publishes to this channel
channel->get_subscribe_count(); // Total subscribes
channel->get_unsubscribe_count(); // Total unsubscribesChannel API
Listing and Finding Channels
cpp
// List all channels
auto channels = clients.get_channels();
for (auto& ch : channels) {
fmt::println("{}: {} ({})", ch->get_name(), ch->get_type(), ch->get_id());
}
// Find a channel by name
auto ch = clients.get_channel_by_name("room-general");
// Find a channel by ID
auto ch = clients.get_channel_by_id(some_uuid);Channel Connections
cpp
// Get all WebSocket connections subscribed to a channel
auto connections = clients.get_websocket_connections_by_node_id_and_channel(
node_id, "room-general");
// List all active WebSocket connections
auto all_ws = clients.get_websocket_connections();Removing Empty Channels
cpp
// Remove a channel if it has no subscribers
clients.try_remove_channel(channel_id);Getting Channel Members
For presence channels, get the list of connected member session IDs:
cpp
auto members = clients.get_channel_members(channel_id);Broadcasting to a Channel
Send a message to all subscribers of a channel:
cpp
clients.broadcast_to_channel(
node_id, // Local node ID
"room-general", // Channel name
frame, // shared_buffer with the message
std::nullopt, // exclude_id (optional)
false, // local_only — if true, don't propagate to other nodes
instance_ptr // state instance for cross-node propagation
);The Subscription Flow
- Client sends an
Opcode_ClientSubscribemessage via WebSocket with the channel name. - The built-in
handlers::subscribehandler processes the message. - The framework checks authorization: private/presence channels require a JWT grant.
clients::registry::add_subscription()registers the subscription.- When a message is published,
clients::broadcast_to_channel()delivers it to all subscribers. - On disconnect,
remove_subscriptions_by_session_id()cleans up automatically.