Back

how to subscribe collection

  • 0
  • Databases
  • Flutter
  • Realtime
conqueror
12 Jun, 2023, 13:36

how to subscribe collection

TL;DR
To subscribe to a collection using Appwrite's Realtime feature, follow these steps: 1. Import the necessary dependencies in your project: ```dart import 'package:appwrite/appwrite.dart'; ``` 2. Initialize the Appwrite client with the endpoint and project ID: ```dart final client = Client() .setEndpoint('https://cloud.appwrite.io/v1') .setProject('[PROJECT_ID]'); ``` Replace `[PROJECT_ID]` with your actual project ID. 3. Create an instance of the Realtime class: ```dart final realtime = Realtime(client); ``` 4. Subscribe to the
Binyamin
12 Jun, 2023, 13:47

You can, For example

TypeScript
import 'package:appwrite/appwrite.dart';

final client = Client()
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('[PROJECT_ID]');

final realtime = Realtime(client);

final subscription = realtime.subscribe(['databases.DB_ID.collections.COLLECTION_ID.documents']);

subscription.stream.listen((response) {
    // Callback will be executed on all account events.
    print(response);
})

Replace DB_ID with your database ID, and COLLECTION_ID with your collection ID.

Read more here: https://appwrite.io/docs/realtime

hortigado
14 Jun, 2023, 15:44

Controller void suscribe(String postId) { if (subscription == null) { subscription = realtime.subscribe([ "databases.${AppWrite.dbId}.collections.${AppWrite.commentsCollectionId}.documents" ]); subscription!.stream.listen((event) { if (event.payload.isNotEmpty) { CommentModel commentModel = CommentModel.fromJson(event.payload); if (commentModel.postId == postId) { commentsList.add(commentModel); } } }); } }

hortigado
14 Jun, 2023, 15:44

main.dart

hortigado
14 Jun, 2023, 15:45
TypeScript
    .setEndpoint('https://cloud.appwrite.io/v1')
    .setProject('[PROJECT_ID]');

final realtime = Realtime(client);```
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more