* Subscribes to Appwrite events and passes you the payload in realtime. * * @param {string|string[]} channels * Channel to subscribe - pass a single channel as a string or multiple with an array of strings. * * Possible channels are: * - account * - collections
(
channels: string | string[],
callback: (payload: RealtimeResponseEvent<T>) => void,
queries: (string | Query)[] = []
)
| 468 | * @returns {() => void} Unsubscribes from events. |
| 469 | */ |
| 470 | subscribe<T extends unknown>( |
| 471 | channels: string | string[], |
| 472 | callback: (payload: RealtimeResponseEvent<T>) => void, |
| 473 | queries: (string | Query)[] = [] |
| 474 | ): () => void { |
| 475 | let channelArray = typeof channels === 'string' ? [channels] : channels; |
| 476 | channelArray.forEach(channel => this.realtime.channels.add(channel)); |
| 477 | |
| 478 | const queryStrings = (queries ?? []).map(q => typeof q === 'string' ? q : q.toString()); |
| 479 | queryStrings.forEach(query => this.realtime.queries.add(query)); |
| 480 | |
| 481 | const counter = this.realtime.subscriptionsCounter++; |
| 482 | this.realtime.subscriptions.set(counter, { |
| 483 | channels: channelArray, |
| 484 | queries: queryStrings, |
| 485 | callback |
| 486 | }); |
| 487 | |
| 488 | this.realtime.connect(); |
| 489 | |
| 490 | return () => { |
| 491 | this.realtime.subscriptions.delete(counter); |
| 492 | this.realtime.cleanUp(channelArray, queryStrings); |
| 493 | this.realtime.connect(); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | async call(method: string, url: URL, headers: Headers = {}, params: Payload = {}, responseType = 'json'): Promise<any> { |
| 498 | method = method.toUpperCase(); |