The Subscriber API lets clients subscribe to channels and resources to receive messages in real time. Subscribers with suitable permissions instantly receive messages sent by a publisher. Channels and resources are independent, so messages sent to a channel aren't delivered to a resource, and vice versa.
Learn more about the Realtime APIs.
With the Subscriber API, you can:
The subscribe() method returns a subscription ID immediately, but the connection is established in the background. Subscribers can use the subscription ID to unsubscribe at a later time.
You can register lifecycle handlers to respond when the subscription succeeds or fails:
onSubscribed handler runs on every successful connection, including automatic re-connections attempted after network interruptions.onSubscriptionError handler runs whenever an error occurs.If the connection drops, the client automatically attempts to reconnect and re-subscribe. When subscribing or re-subscribing, the isSynced parameter of the onSubscribed() handler indicates whether all missed messages were successfully restored, or whether the subscriber needs to take further action.
Learn more about subscribing to a channel or resource.
Subscription errors occur when a subscription is rejected or when the connection fails. Each error includes errorCode and recoverable parameters that indicate the error type and whether the client is automatically trying to reconnect.
Learn more about how the subscribe() method handles subscription errors.
You can unsubscribe in the following ways:
subscriptionId: Removes a single subscription.channel: Removes all subscriptions that match the specified channel and resource exactly. Unsubscribing from a channel doesn't affect subscriptions to resources on that channel, and vice versa.When the last subscription on a connection is removed, the connection closes automatically.
It's important to note the following points before starting to code:
read permission on the channel, the subscription fails.