npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactive-mongo

v0.0.2

Published

Event listening for MongoDB

Downloads

1

Readme

Reactive Mongo

This module allows you to subscribe to events, based on what's shakin' in your MongoDB.

NPM

npm install reactive-mongo --save

Usage

Create a connection to your database (local or remote) and begin reacting to changes in the oplog.

var Reactive = require('reactive-mongo');
var Db = new Reactive(); // assumes local, unless URI passed

Db.on('error', function(err) {
  console.log(err);
});

var reference = new Db('testdb.records');

reference.on('insert', function(doc) {
  console.log(doc);
});

reference.on('update', function(doc) {
  console.log(doc);
});

reference.on('delete', function(doc) {
  console.log(doc);
});

reference.on('op', function(op, doc) {
  console.log('operation: ' + op);
  console.log(doc);
});

On any reference, you can also retrieve the query and raw operation object.

reference.on('update', function(doc, query, op) {
  // attributes updated
  console.log(doc);
  // query used in find
  console.log(query);
  // raw oplog operation
  console.log(op);
});

How?

MongoDB uses an oplog (short for "operation log") to sync a master to its slaves. Most databases have some form of replication files.

Working on local

If you're on a local instance of MongoDB, you'll likely have to configure it to write to the oplog.

First run MongoDB with the following settings

$ sudo mongod --replSet rs0 --oplogSize 100

In the same terminal you'll probably see a repeating error.

2014-09-08T15:55:16.748-0700 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-09-08T15:55:17.749-0700 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-09-08T15:55:18.750-0700 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-09-08T15:55:19.751-0700 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
2014-09-08T15:55:20.752-0700 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

This is fine. Just temporary.

Now, begin a Mongo client connection, and do the following.

$ mongo
> use local
switched to db local
> rs.initiate()
{
  "info2" : "no configuration explicitly specified -- making one",
  "me" : "Your-MacBook-Pro.local:27017",
  "info" : "Config now saved locally.  Should come online in about a minute.",
  "ok" : 1
}

Now, in the same client, if you run show collections you should see some new ones for the oplog.

Go back to the mongod terminal and restart it. The errors should go away, and you'll be ready to use Reactive Mongo.

Working on Compose (formerlly MongoHQ)

You must use a database that has elastic search turned on.

Use a database user who has oplogAccess enabled.

oplogAccess user creation example

In the settings for the database, you'll see an example of how to connect to the database. It will look something like

mongodb://<user>:<password>@candidate.35.mongolayer.com:10491,candidate.34.mongolayer.com:10493/oplog_test

In this example oplog_test is the database name. Tweak that slightly. Instead of ending in /oplog_test change it to be /local?authSource=oplog_test.

mongodb://<user>:<password>@candidate.35.mongolayer.com:10491,candidate.34.mongolayer.com:10493/local?authSource=oplog_test

Use this path to connect to your database and tail the oplog.