The Appwrite Realtime API lets you subscribe to events from any Appwrite service through channels. You can subscribe to a single channel, multiple channels at once, and unsubscribe when you no longer need updates. On supported client SDKs (including the Web SDK), multiple subscriptions share one WebSocket and can be added, updated, or removed without reconnecting the whole client until you call disconnect() on Realtime.
Subscribe to a channel
In this example we are subscribing to all updates related to our account by using the account channel. This will be triggered by any update related to the authenticated user, like updating the user's name or e-mail address.
Subscribe to multiple channels
You can also listen to multiple channels at once by passing an array of channels. This will trigger the callback for any events for all channels passed.
In this example we are listening to a specific row and all files by subscribing to Channel.tablesdb("<DATABASE_ID>").table("<TABLE_ID>").row("<ROW_ID>") and Channel.files() channels.
Update channels or queries
Channels and queries on an active subscription can be replaced in place without recreating the WebSocket. This is useful when, for example, a user changes which row they're viewing. Swap the channel on the existing subscription instead of unsubscribing and resubscribing.
update() accepts either or both of channels and queries. Pass only the field you want to replace; omitted fields are left unchanged.
Unsubscribe
If you no longer want to receive updates from a particular subscription, call unsubscribe() on it. Other subscriptions and the underlying WebSocket connection are not affected, so callbacks for the rest of your app keep firing. To close the entire connection at once, see Disconnect below.
Legacy close()
subscription.close() still works for backwards compatibility. It calls unsubscribe() and additionally closes the WebSocket if this was the last active subscription. New code should prefer unsubscribe() and call realtime.disconnect() explicitly when full teardown is needed.
Disconnect
Call realtime.disconnect() to drop all active subscriptions and close the WebSocket in one step. Use this at app teardown or when a user logs out.