Augmenting observer reuse in Meteor
kwak.io is an ephemeral chat. Ephemeral means your messages only last a while, then they get deleted. It also means that when you enter a chat room, a Meteor subscription is made to the messages that have been posted after someTimeAgo.
Roughly, this is what we were initially doing:
Observer reuse is an important Meteor concept. Every subscription comes with an observer on this subscription. Every different subscription is a new burden on Node and Mongo, and having lots of different subscription affects their performance and the pubsub response time. Here is a good read on this matter. By the way, kadira is an awesome Meteor monitoring tool which shows observer reuse.
Back to our problem: with the above code, when two users connect to kwak at a time that is different at the millisecond granularity, they will create different subscriptions and observers. Hence no observer reuse unless two people subscribe at the exact same millisecond, which is highly improbable and would still make for a low observer reuse score.
To solve this problem, we introduced a function we called ceilToFive, which takes a time in ms and rounds it to the next five minutes. This way, all people accessing kwak.io in the same five minute window will get the same subscription, and our observer reuse will rise while still maintaining a good sense of ephemerality.