Realtime

Appwrite supports multiple protocols for accessing the server, including REST, GraphQL, and Realtime. The Appwrite Realtime allows you to listen to any Appwrite events in realtime using the subscribe method.

Instead of requesting new data via HTTP, the subscription will receive new data every time it changes, any connected client receives that update within milliseconds via a WebSocket connection.

This lets you build an interactive and responsive user experience by providing information from all of Appwrite's services in realtime. The example below shows subscribing to realtime events for file uploads.

import { Client } from "appwrite";

const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

// Subscribe to files channel
client.subscribe('files', response => {
    if(response.events.includes('buckets.*.files.*.create')) {
        // Log when a new file is uploaded
        console.log(response.payload);
    }
});

To subscribe to updates from different Appwrite resources, you need to specify one or more channels. The channels offer a wide and powerful selection that will allow you to listen to all possible resources. This allows you to receive updates not only from the database, but from all the services that Appwrite offers.

If you subscribe to a channel, you will receive callbacks for a variety of events related to the channel. The events attribute in the callback can be used to filter and respond to specific events in a channel.

View a list of all available events.

Permissions

All subscriptions are secured by the permissions system offered by Appwrite, meaning a user will only receive updates to resources they have permission to access.

Using Role.any() on read permissions will allow any client to receive updates.

Authentication

Realtime authenticates using an existing user session. If you authenticate after creating a subscription, the subscription will not receive updates for the newly authenticated user. You will need to re-create the subscription to work with the new user.

More information and examples of authenticating users can be found in the dedicated authentication docs.

Examples

The examples below will show you how you can use Realtime in various ways.

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.

import { Client } from "appwrite";

const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

client.subscribe('account', response => {
    // Callback will be executed on all account events.
    console.log(response);
});

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 the document A and all files by subscribing to the databases.A.collections.A.documents.A and files channels.

import { Client } from "appwrite";

const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

client.subscribe(['collections.A.documents.A', 'files'], response => {
    // Callback will be executed on changes for documents A and all files.
    console.log(response);
});

Unsubscribe

If you no longer want to receive updates from a subscription, you can unsubscribe so that your callbacks are no longer called. Leaving old subscriptions alive and resubscribing can result in duplicate subscriptions and cause race conditions.

import { Client } from "appwrite";

const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

const unsubscribe = client.subscribe('files', response => {
    // Callback will be executed on changes for all files.
    console.log(response);
});

// Closes the subscription.
unsubscribe();

Payload

The payload from the subscription will contain following properties:

NameTypeDescription
eventsstring[]The Appwrite events that triggered this update.
channelsstring[]An array of channels that can receive this message.
timestampstringThe ISO 8601 timestamp in UTC timezone from the server
payloadobjectPayload contains the data equal to the response model.

If you subscribe to the documents channel and a document the user is allowed to read is updated, you will receive an object containing information about the event and the updated document.

The response will look like this:

JSON
{
  "events": [
    "databases.default.collections.sample.documents.63c98b9baea0938e1206.update",
    "databases.*.collections.*.documents.*.update",
    "databases.default.collections.*.documents.63c98b9baea0938e1206.update",
    "databases.*.collections.*.documents.63c98b9baea0938e1206.update",
    "databases.*.collections.sample.documents.63c98b9baea0938e1206.update",
    "databases.default.collections.sample.documents.*.update",
    "databases.*.collections.sample.documents.*.update",
    "databases.default.collections.*.documents.*.update",
    "databases.default.collections.sample.documents.63c98b9baea0938e1206",
    "databases.*.collections.*.documents.*",
    "databases.default.collections.*.documents.63c98b9baea0938e1206",
    "databases.*.collections.*.documents.63c98b9baea0938e1206",
    "databases.*.collections.sample.documents.63c98b9baea0938e1206",
    "databases.default.collections.sample.documents.*",
    "databases.*.collections.sample.documents.*",
    "databases.default.collections.*.documents.*",
    "databases.default.collections.sample",
    "databases.*.collections.*",
    "databases.default.collections.*",
    "databases.*.collections.sample",
    "databases.default",
    "databases.*"
  ],
  "channels": [
    "documents",
    "databases.default.collections.sample.documents",
    "databases.default.collections.sample.documents.63c98b9baea0938e1206"
  ],
  "timestamp": "2023-01-19 18:30:04.051",
  "payload": {
    "ip": "127.0.0.1",
    "stringArray": [
      "sss"
    ],
    "email": "joe@example.com",
    "stringRequired": "req",
    "float": 3.3,
    "boolean": false,
    "integer": 3,
    "enum": "apple",
    "stringDefault": "default",
    "datetime": "2023-01-19T10:27:09.428+00:00",
    "url": "https://appwrite.io",
    "$id": "63c98b9baea0938e1206",
    "$createdAt": "2023-01-19T18:27:39.715+00:00",
    "$updatedAt": "2023-01-19T18:30:04.040+00:00",
    "$permissions": [],
    "$collectionId": "sample",
    "$databaseId": "default"
  }
}

Channels

A list of all channels available you can subscribe to. IDs cannot be wildcards.

ChannelDescription
accountAll account related events (session create, name update...)
databases.[ID].collections.[ID].documentsAny create/update/delete events to any document in a collection
documentsAny create/update/delete events to any document
databases.[ID].collections.[ID].documents.[ID]Any update/delete events to a given document
filesAny create/update/delete events to any file
buckets.[ID].files.[ID]Any update/delete events to a given file of the given bucket
buckets.[ID].filesAny update/delete events to any file of the given bucket
teamsAny create/update/delete events to a any team
teams.[ID]Any update/delete events to a given team
membershipsAny create/update/delete events to a any membership
memberships.[ID]Any update/delete events to a given membership
executionsAny update to executions
executions.[ID]Any update to a given execution
functions.[ID]Any execution event to a given function

Custom endpoint

The SDK will guess the endpoint of the Realtime API when setting the endpoint of your Appwrite instance. If you are running Appwrite with a custom proxy and changed the route of the Realtime API, you can call the setEndpointRealtime method on the Client SDK and set your new endpoint value.

By default the endpoint is wss://cloud.appwrite.io/v1/realtime.

import { Client } from "appwrite";
const client = new Client();

client.setEndpointRealtime('wss://cloud.appwrite.io/v1/realtime');

Limitations

While the Realtime API offers robust capabilities, there are currently some limitations to be aware of in its implementation.

Subscription changes

The SDK creates a single WebSocket connection for all subscribed channels. Each time a channel is added or unsubscribed, the SDK currently creates a completely new connection and terminates the old one. Therefore, subscriptions to channels should always be done in conjunction with state management so as not to be unnecessarily built up several times by multiple components' life cycles.

Server SDKs

We currently are not offering access to realtime with Server SDKs and an API key.