Skip to main content
Clients subscribe to a channel to receive the messages published to it. Clients can subscribe to all messages, or only messages identified by specific names. Subscribing is an operation that is only available to the realtime interface of Pub/Sub SDKs. This is because it requires establishing a persistent connection to Ably in order to receive messages in realtime.

Subscribe to all messages

Use the subscribe() method on a channel to receive any messages that are published to it. The following is an example of subscribing to all messages on a channel:

Subscribe to specific message names

You can subscribe to only messages with specific names:

Subscribe to multiple message names

You can register multiple listeners for different message names:

Unsubscribe from messages

You can unsubscribe from messages to stop receiving updates:

Unsubscribe from specific message names

Working with message data

Messages contain several useful properties:

Handling connection state

It’s good practice to handle connection state changes:

Handling channel state

Channels also have state that you can monitor:

Automatic reattachment

If the connection is lost, the Ably SDK will automatically attempt to reconnect and reattach to channels. Messages published during the disconnection will be delivered once the connection is restored (up to 2 minutes of message history).

Next steps

Build docs developers (and LLMs) love