Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

meteor

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.

  • Where will be my observer function executed? By Meteor or by mongo?

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.

  • How can I implement my observe to it be like a singleton? Just one instance running for all.

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

like image 718
zVictor Avatar asked Feb 18 '23 16:02

zVictor


1 Answers

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.

like image 103
Tom Coleman Avatar answered Apr 30 '23 02:04

Tom Coleman