Channels
Channels are the core primitive for organizing realtime messages. Every event is published to one or more channels.
Public Channels
Any client can subscribe without authentication.
const channel = relay.subscribe('news');
channel.bind('breaking', (data) => console.log(data));
Private Channels
Prefixed with private-. Require server-side authentication before subscribing.
const channel = relay.subscribe('private-user-123');
channel.bind('notification', (data) => console.log(data));
The client automatically calls your auth endpoint (/broadcasting/auth or configured authEndpoint) with the socket_id and channel_name. Your server validates access and returns an HMAC signature.
Presence Channels
Prefixed with presence-. Like private channels, but also track who's online.
const channel = relay.subscribe('presence-room-42');
channel.bind('pusher:subscription_succeeded', (members) => {
console.log('Online:', members.count);
});
channel.bind('pusher:member_added', (member) => {
console.log('Joined:', member.id);
});
channel.bind('pusher:member_removed', (member) => {
console.log('Left:', member.id);
});
Your auth endpoint must return channel_data with user_id and optional user_info:
{
"auth": "key:signature",
"channel_data": "{\"user_id\":\"123\",\"user_info\":{\"name\":\"Tom\"}}"
}
Cache Channels
Prefixed with cache-. The server remembers the last event and delivers it immediately when a new client subscribes. Great for showing current state.
const channel = relay.subscribe('cache-stock-prices');
channel.bind('update', (data) => renderPrices(data));
Client Events
Clients can send events directly to other subscribers on private/presence channels. Event names must start with client-.
// On private or presence channels only
channel.trigger('client-typing', { user: 'Tom' });
Channel Naming
- Max 200 characters
- No spaces
- Prefix determines type:
private-,presence-,cache-,no-history- no-history-prefix disables message history storage