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

node-mongotailableevents

v0.0.3

Published

Event bus interface based on mongodb tailable cursors

Downloads

3

Readme

Module for creating event bus interface based on MongoDB tailable cursors

The idea behind this module is to create EventEmitter like interface, which uses MongoDB capped collections and tailable cursors as an internal messaging bus. This model has a lot of advantages, especially if you already use MongoDB in your project.

The advantages are:

You don't have to exchange the event emitter object between different pages or even different processes (forked, clustered, living on separate machines). As long as you use the same mongoUrl and capped collection name, you can exchange information. This way you can even create applications that runs on a different hardware and they may exchanging events and data as if they are the same application! Also your events are stored in a collection and could be used as a transaction log latley (mongodb's own transaction log is implemented with capped collections).

It simplifies an application development very much.

Installation

To install the module run the following command:

npm install node-mongotailableevents

Short

It is easy to use that module. Look at the following example:

var ev = require('node-mongotailableevents');

var e = ev( { ...options ... }, callback );

e.on('event',callback);

e.emit('event',data);

Initialization and options

The following options can be used with the module

  • mongoUrl (default mongodb://127.0.0.1/test) - the URL to the mongo database
  • mongoOptions (default none) - Specific options to be used for the connection to the mongo database
  • name (default tailedEvents) - the name of the capped collection that will be created if it does not exists
  • size (default 1000000) - the maximum size of the capped collection (when reached, the oldest records will be automatically removed)
  • max (default 1000) - the maximum size in amount of records for the capped collection

You can call and create a new event emitter instance without options:

var ev = require('node-mongotailableevents');
var e = ev();

Or you can call and create a event emitter instance with options:

var ev = require('node-mongotailableevents');
var e = ev({
   mongoUrl: 'mongodb://127.0.0.1/mydb',
   name: 'myEventCollection'
});

Or you can call and create a event emitter instance with options and callback, which will be called when the collection is created successfuly:

var ev = require('node-mongotailableevents');
ev({
   mongoUrl: 'mongodb://127.0.0.1/mydb',
   name: 'myEventCollection'
}, function(err, e) {
    console.log('EventEmitter',e);
});

Or you can call and create event emitter with just callback (and default options):

ev(function(err, e) {
    console.log('EventEmitter',e);
});

Usage

This module inherits EventEmitter, so you can use all of the EventEmitter methods. Example:

ev(function(err, e) {
    if (err) throw err;
    
    e.on('myevent',function(data) {
        console.log('We have received',data);
    });
    
    e.emit('myevent','my data');
});

The best feature is that you can exchange events between different pages or processes, without the need of exchange in advance of the eventEmitter object instance or without any complex configuration, as long as both pages processes uses the same mongodb database (but it could be a different replica servers) and the same "name" (the name of the capped collection). This way you can create massive clusters and messaging bus distributed among multiple machines without a need of any separate messaging system and its configuration.

Do a simple example - start two separate node processes with the following code, and see what the results are:

var ev = require('node-mongotailableevents');
ev(function(err, e) {
    if (err) throw err;
    
    e.on('myevent',function(data) {
        console.log('We have received',data);
    });
    
    setInterval(function() {
        e.emit('myevent','my data'+parseInt(Math.random()*1000000));
    },5000);
});

You shall see on both of the outputs both of the messages received.