RealtimeSubscription not working if document is being listened to multiple times by the same client
- 0
- Self Hosted
- Flutter
- Realtime
i can create additional documents and get the new event to come in. i can update an existing document and see 2 events come in (1 for the subscription on all documents and 1 for just the single document)
Hmmm okay. What if the number of documents I have subscribed to has something to do with the bug reproducibility? I have like ~700 documents on a stream. The moment I take one document and add one additional stream on it (exclusively for itself), the second stream becomes an on-off situation it works and fails to work at times
the number of documents don't really have an effect...the number of realtime subscriptions might
I tinkered around this issue for quite a while and I was able to reproduce it more than 10 times
I used different approaches and state management logics
The bug is reproducible on my part
I think there might be a time interval between a stream's listen and dispose at the backend
because I noticed if I disposed the second stream and subscribed to it within 5 seconds, it almost always failed to work
there might be some logic on the backend to not process a request until a current one is disposed of
and even on the docker realtime logs, i was able to see an open connection yet no response on the client
so i tested again w/ 2 buttons. 1 subscribe on all documents. Another button calls list documents and then subscribes on each of those individual docuemnts. i updated 1 document from the console and then i received 2 events
another test with automatically subscribing instead of subscribing with button clicks and it works as expected
@Steven I appreciate the time you took to look into this. I want to show you the following video of a test I just performed. The first screen is the list of documents and the second one is the view of that document both screen deploy a stream and every time screen 2 is closed the stream will disposed. The document change is an automated task and is done thru a dart client
*Riverpod used to view the model * https://pastebin.com/3sp0cCkw
Repository and methods https://pastebin.com/wTpnXDty
Database class https://pastebin.com/yTKBB6zz
Based on my observation I think that there is a ~5 second interval between accepting a stream and its disposal because if I dispose a stream and start the same one within 5 seconds it seems to almost always fail to work
as you can see from the video
where and how is Realtime initialized?
I have initialized it as follows but it does not make a difference if I followed the way the doc instantiates it
Client client() => _client;
Realtime get realtime => Realtime(_client);
I am able to reproduce the issue regardless of how I instantiate the Realtime object
can you share the full code around this?
Absolutely ```import 'package:appwrite/appwrite.dart'; import 'package:appwrite/models.dart'; Client _client = Client().setEndpoint(_endpoint).setProject(_projectID);
Client client() => _client;
Future<User> getCurrentUser() async => Account(_client).get().then((value) => value);
Future<Preferences> getAccountPrefs() async => getCurrentUser().then((value) => value.prefs);
Account getAccountService() => Account(_client);
Databases get databases => Databases(_client);
Realtime get realtime => Realtime(_client);
Storage get storage => Storage(_client);
Functions get functions => Functions(_client);
usage of my realtime getter
RealtimeSubscription getDoctorRealtimeSubscription(String doctorDocumentId) { try { return realtime.subscribe([ 'databases.$_doctorListDatabaseId.collections.$_doctorListCollectionId.documents.$doctorDocumentId' ]); } catch (e) { if (kDebugMode) print('getDoctorRealtimeSubscription: $e'); rethrow; } }```
Using the RealtimeSub in riverpod
void _startRealtimeSubscription() {
_realtimeSubscription = _setDoctorRepository
.getDoctorModelRealtimeSubscription(doctorModel.documentId);
_realtimeSubscription?.stream.listen(_onRealtimeMessage,
onError: _onStreamError, onDone: _onStreamDone);
}```
if (message.events.contains(documentDeletedEvent)) {
state = const AsyncData(DoctorModel());
} else {
final result =
_setDoctorRepository.getDoctorModelFromRealtimeMsg(message);
if (result != null) state = AsyncData(result);
}
}```
When the issue occurs non of the supplied methods to the stream.listen
will get called
even though the IDE's console prints out about a realtime subscription and the docker logs show that connection
Steven did you conclude anything
Recommended threads
- Send Email Verification With REST
I am using REST to create a user on the server side after receiving form data from the client. After the account is successfully created i wanted to send the v...
- Use different email hosts for different ...
Hello, I have 2 projects and i want to be able to set up email templates in the projects. Both projects will have different email host configurations. I see ...
- Migrate from cloud to localhost
Hello everyone. I need to migrate my test project from cloud to localhost, however it seems that this is possible only if a self-hosted appwrite instance it's h...