Skip to main content
Channels are the primary mechanism for organizing and routing messages in Ably. Each channel provides publish/subscribe functionality, presence, and history retrieval.

Channels Collection

Access channels through the channels property:

get

channels.get(name, options?): Channel Get or create a channel instance.
name
string
required
Channel name. Use : to separate namespaces (for example, chat:room1).
options
object
Channel options:
  • params (object): Channel parameters for filtering
  • modes (array): Channel modes (for example, ['PUBLISH', 'SUBSCRIBE'])
  • cipher (object): Encryption configuration

release

channels.release(name): void Release a channel and remove all listeners.

Channel Object

A channel instance provides methods for publishing, subscribing, and managing channel state.

Properties

name
string
The channel name.
state
string
Current channel state: initialized, attaching, attached, detaching, detached, or failed.
errorReason
object
Error information if channel is in failed state.
presence
object
Presence object for this channel. See Presence API.

publish

publish(name?, data?, callback?): Promise<void> Publish a message to the channel.
name
string
Optional event name for the message.
data
any
Message payload. Supported types: string, JSON object, binary data.
callback
function
Optional callback function called on completion.

subscribe

subscribe(eventName?, listener): Promise<void> Subscribe to messages on the channel.
eventName
string
Optional event name to filter messages.
listener
function
required
Callback function called for each message.

unsubscribe

unsubscribe(eventName?, listener?): void Unsubscribe from messages.

attach

attach(callback?): Promise<void> Explicitly attach to the channel.

detach

detach(callback?): Promise<void> Detach from the channel.

history

history(options?): Promise<PaginatedResult<Message>> Retrieve message history for the channel.
start
integer
Start time in milliseconds since epoch.
end
integer
End time in milliseconds since epoch.
limit
integer
default:"100"
Maximum number of messages to return (max 1000).
direction
string
default:"backwards"
Query direction: forwards or backwards.

Channel Events

Subscribe to channel state changes: Channel states:
  • initialized - Channel created but not attached
  • attaching - Attachment in progress
  • attached - Successfully attached
  • detaching - Detachment in progress
  • detached - Detached from channel
  • failed - Attachment failed

Build docs developers (and LLMs) love