Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

publish-counts

> 编程语言
Open source

Meteor package to help you publish the count of a cursor, in real time

200 stars0 likes1 views
WebsiteGitHub

About

Meteor package to help you publish the count of a cursor, in real time

Publish Counts

A package to help you publish the count of a cursor, in real time.

Publish-counts is designed for counting a small number of documents around an order of 100. Due to the real-time capability, this package should not be used to count all documents in large datasets. Maybe some, but not all. Otherwise, you will maximize your server's CPU usage as each client connects.

v1.x Breaking changes

This version is only compatible with Meteor 3.0 and higher.

Table of Contents

  • Installation
  • API
    • Counts.publish
    • Counts.get
    • Counts.has
    • Counts.noWarnings
  • Options
    • noReady
    • nonReactive
    • countFromField
    • countFromFieldLength
    • noWarnings
  • Template helpers
  • Notes
    • Observer handle leak testing
    • Why doesn't this library count directly in Mongo? or...
    • Scalable count packages
    • [Compatiblity with Meteor Counts.publish this, 'name-of-counter', Posts.find() return undefined # otherwise coffeescript returns a Counts.publish # handle when Meteor expects a Mongo.Cursor object.
…

js Meteor.publish('posts-with-count', function() { Counts.publish(this, 'posts', Posts.find(), { noReady: true }); return Posts.find({}, { limit: 10 }); });


### nonReactive

If you specify `{nonReactive: true}` the cursor won't be observed and only the initial count will be sent on initially subscribing. This is useful in some cases where reactivity is not desired, and can improve performance.

### countFromField

`countFromField` allows you to specify a field to calculate the sum of its numbers across all documents.
For example if we were to store page visits as numbers on a field called `visits`:

{ content: 'testing', visits: 100 }, { content: 'a comment', visits: 50 }


We could then publish them like:

```js
Meteor.publish('posts-visits-count', function() {
  Counts.publish(this, 'posts-visits', Posts.find(), { countFromField: 'visits' });
});

And calling Counts.get('posts-visits') returns 150

If the counter field is deeply nested, e.g.:

{ content: 'testing', stats: { visits: 100 } },
{ content: 'a comment', stats: { visits: 50 } }

Then use an accessor function instead like:

javascript
Meteor.publish('posts-visits-count', function() {
  Counts.publish(this, 'posts-visits',
    Posts.find({}, { fields: { _id: 1, 'stats.visits': 1 }}),
    { countFromField: function (doc) { return doc.stats.visits; } }
  );
});

Note that when using an accessor function, you must limit the fields fetched if desired, otherwise Counts will fetch entire documents as it updates the count.

countFromFieldLength

countFromFieldLength allows you to specify a field to calculate the sum of its length across all documents. For example if we were to store the userIds in an array on a field called likes:

{ content: 'testing', likes: ['6PNw4GQKMA8CLprZf', 'HKv4S7xQ52h6KsXQ7'] },
{ content: 'a comment', likes: ['PSmYXrxpwg276aPf5'] }

We could then publish them like:

javascript
Meteor.publish('posts-likes-count', function() {
  Counts.publish(this, 'posts-likes', Posts.find(), { countFromFieldLength: 'likes' });
});

If the counter field is deeply nested, e.g.:

{ content: 'testing', popularity: { likes: ['6PNw4GQKMA8CLprZf', 'HKv4S7xQ52h6KsXQ7'] } },
{ content: 'a comment', popularity: { likes: ['PSmYXrxpwg276aPf5'] } }

Then use an accessor function instead like:

javascript
Meteor.publish('posts-likes-count', function() {
  Counts.publish(this, 'posts-likes',
    Posts.find({}, { fields: { _id: 1, 'popularity.likes': 1 }}),
    { countFromFieldLength: function (doc) { return doc.popularity.likes; } }
  );
});

…

html

There are {{getPublishedCount 'posts'}} posts.

  {{#if hasPublishedCount 'posts'}}
    There are {{getPublishedCount 'posts'}} posts.
  {{else}}
    The number of posts is loading...
  {{/if}}

Notes

Observer handle leak testing

The package includes a test that checks the number of observer handles opened and closed (to check for memory leaks). You need to run the enable-publication-tests-0.7.0.1 branch of percolatestudio/meteor to run it however.

Why doesn't this library count directly in Mongo? or...

Why does my MongoDB connection time-out with large (1000+) datasets?

This package is designed primarily for correctness, not performance. That's why it's aimed at counting smaller datasets and keeping the count instantly up to date.

To achieve perfect correctness in Meteor data layer, we use a database observer to know immediately if a relevant change has occurred. This approach does not necessarily scale to larger datasets, as the observer needs to cache the entire matching dataset (amongst other reasons).

Counting large datasets in this manner is suspected to cause database connections to time out (see #86).

An alternative approach would be to take a .count() of the relevant cursor (or perform an aggregation in more complex use cases), and poll it regularly to update the count. Bulletproof Meteor provides a proof of concept of this approach in their bullet-counter example.

We'd love to see someone publish a package for this use case! If you do end up making such a package, let us know and we'll link it here.

Scalable Count Packages

  • publish-performant-counts - Performant solution derived directly from bullet-counter.
  • meteor-publish-join - Publish expensive-aggregated values

Compatibility with Meteor < 1.3

Publish-counts 0.8.0 introduces an explicit dependency on the underscore js library which may be incompatible with versions of Meteor below 1.3. Please upgrade Meteor to the latest version or, if you cannot, continue to use publish-counts 0.7.3.

Frequently Asked Questions

More information can be found in the FAQ Section of the issue tracker.

License

MIT. (c) Percolate Studio

publish-counts was developed as part of the Verso project.

Issues· 17 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言