realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

[realm-web] can't close watch stream

Open adam-protempo opened this issue 4 years ago • 2 comments

Goals

Ability to close a watch stream when destroying a component

Expected Results

Watch stream closes and doesn't receive updates.

Actual Results

.close() does not exist and the connection stays active

Steps to Reproduce

  • Create a new watch stream on the collection
  • Destroy component (vue/react)
  • Update a document in the collection and it will still be listening

Code Sample

// Watch for future comments
const mongo = realmUser.mongoClient('mongodb-atlas').db('DATABASE')
const mongoCollection = await mongo.collection('COLLECTION')

for await (const change of mongoCollection.watch({
  ids: [this.request._id],
})) {
  console.log(change)
  this.comments = change.fullDocument.Comments
}

Version of Realm and Tooling

  • realm-web 1.0.0
  • vue 2.6.11

Extra info

This works fine in a previous projects using the Stitch SDK. There you could call .close() on the watch stream. Maybe I'm overlooking something in the new realm-web sdk?

Thanks!

adam-protempo avatar Dec 09 '20 04:12 adam-protempo

Thanks for reporting this. I recently made a comment on another issue explaining how to close the stream.

I agree that the API could be improved and I therefore created #3259 to track that. I'll close this issue as a duplicate of that.

kraenhansen avatar Dec 09 '20 07:12 kraenhansen

Thanks for reporting this. I recently made a comment on another issue explaining how to close the stream.

I agree that the API could be improved and I therefore created #3259 to track that. I'll close this issue as a duplicate of that.

Fixes described in the linked comment don't really work. In the first example break will only be called when there is new data, but there may never be new data. The second example just doesn't work (at least for me) if I set up a setTimeout to return after 5000ms the connection remains open.

Any other suggestions would be great. For now .watch() becomes unusable as you can't close the streams and you can't subscribe to them on react native.

benwalton avatar Feb 07 '21 08:02 benwalton

Any updates on this?

rvanmil avatar Oct 12 '22 15:10 rvanmil

@rvanmil In version 1.7.1 we released a fix related to closing watch streams. It might not be enough but if you haven't upgraded, I suggest you do.

kneth avatar Oct 13 '22 08:10 kneth

Thanks, I must have overlooked that fix. I just ran a quick test and it looks like the connections are properly closed after calling return on the change stream. Thank you 😊

rvanmil avatar Oct 13 '22 11:10 rvanmil