You can filter realtime events by passing queries as a third parameter when subscribing. Events are filtered server-side based on your queries, so your callback only receives updates that match your conditions. This allows you to use familiar SDK queries like Query.equal to automatically filter events instead of filtering manually in your callback.
import { Client, Realtime, Channel, Query } from "appwrite";
const client = new Client()
.setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
.setProject('<PROJECT_ID>');
const realtime = new Realtime(client);
// Subscribe to all updates
const allVotes = await realtime.subscribe(
Channel.tablesdb('<DATABASE_ID>').table('<TABLE_ID>').row(),
response => {
console.log(response.payload);
}
);
// Subscribe to updates where person equals 'person1'
const person1Votes = await realtime.subscribe(
Channel.tablesdb('<DATABASE_ID>').table('<TABLE_ID>').row(),
response => {
console.log(response.payload);
},
[Query.equal('person', ['person1'])]
);
// Subscribe to updates where person is not 'person1'
const otherVotes = await realtime.subscribe(
Channel.tablesdb('<DATABASE_ID>').table('<TABLE_ID>').row(),
response => {
console.log(response.payload);
},
[Query.notEqual('person', 'person1')]
);
Supported queries
The following query methods are supported for realtime filtering:
| Category | Queries |
Comparison | Query.equal(), Query.notEqual(), Query.greaterThan(), Query.greaterThanEqual(), Query.lessThan(), Query.lessThanEqual() |
Null checks | Query.isNull(), Query.isNotNull() |
Logical | Query.and(), Query.or() |