zVictor
zVictor

Reputation: 3717

How cursor.observe works and how to avoid multiple instances running?

Observe

I was trying to figure it out how cursor.observe runs inside meteor, but found nothing about it. Docs says

Establishes a live query that notifies callbacks on any change to the query result.

I would like to understand better what live query means.

Multiple runs

When we have more than just a user subscribing an observer, one instance runs for each client, leading us to a performance and race condition issue.

Edit: There was a third question here, but now it is a separated question: How to avoid race conditions on cursor.observe?

Upvotes: 4

Views: 2446

Answers (1)

Tom Coleman
Tom Coleman

Reputation: 3037

Server side, as of right now, observe works as follows:

  1. Construct the set of documents that match the query.
  2. Regularly poll the database with query and take a diff of the changes, emitting the relevant events to the callbacks.
  3. When matching data is changed/inserted into mongo by meteor itself, emit the relevant events, short circuiting step #2 above.

There are plans (possibly in the next release) to automatically ensure that calls to subscribe that have the same arguments are shared. So basically taking care of the singleton part for you automatically.

Certainly you could achieve something like this yourself, but I believe it's a high priority for the meteor team, so it's probably not worth the effort at this point.

Upvotes: 9

Related Questions