Realm WEB SDK collection watch and readConcern

Hi there,

I have an issue with watch() function fo realm web sdk collections.

When I insert a new document in the collection, the change event get trapped and I make a
collection.find() to update the list of document. The issue is that the newly created document
is not present in the find() result.

I think that the issue is related to Atlas cluster default read and write concerns…
i have a mongodb 5.0 cluster M10 and the default readConcern is local while the
default write concern is majority. I was unable to modify the default readConcern and
I have not found anything about specifyng readConcern during find() operation (web sdk).

here is a snippet of code to clarify what I’m tryng to do:

private async initWatcher() {

    if (this.debug) console.log("[SiteService] - initWatcher - checking existent watcher...");
    // closing the existing watcher to avoid memory leaks
    if (this.watcher) this.watcher.return(undefined);
    if (this.debug) console.log("[SiteService] - initWatcher - defining new watcher...");

    // creating the new watcher  
    this.watcher = this._sitesCollection.watch();

    // listening for changes
    for await (const change of this.watcher) {

      if (this.debug) console.log("[SiteService] - initWatcher - watch operationType: ", change.operationType);
      await this.fetchSites();
      await this.fetchDashboardSites();
    }
  }

I think that I filnally managed to solve the issue.

The problem was due to the sync functionality paired with the WEB SDK use of watch().

I think that the WEB SDK uses the GraphQL API or the Data API under the hood, so when a call is done by the client, a JWT token is passed in the bearer of the http call.
I noticed that the JWT token includes the custom user data, writePartition array included, that is the list of partitions the user have access to. This token remains the same until it expires.
Every time I create a new site, I generate a new partition and push that partition to the writePartitions array of the user’s custom data collections, but the JWT token stored in localstorage of the client does not have this newly created partition in his writePartitions array.
This cause the subsequent http calls to transfer a wrong list of partition to the server that returns a wrong list of sites inside the watch() functions.

I solved this adding a forced refresh of the access token before the find() call:


await this._realmSvc.currentUser.refreshAccessToken();


after this, all became working like a charm.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.