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

mongo-brocast

v0.0.3

Published

Pub/Sub with MongoDB. A `Brocast` implementation.

Downloads

1

Readme

brocast

Pub/Sub with MongoDB. A Brocast implementation.

Reactive, and easily extensible.

This implementation is EventStore compatible, as you can subscribe to past events.

Usage

  var Brocast = require('mongo-brocast').Brocast;

var pubsub = new Brocast(db, opts)

Args:

  • db - Instance of Db() from MongoDB's native driver.
  • opts - Object with Options.

Options:

  • channelSize - (default=16MB) Max amount of bytes for each channel. If a channel will exceed this size, new events will overwrite old ones.
  • maxEventsPerChannel - (default=1000) If your channel is less than channelSize, what's the max amount of events to store.
  • ns - (default='pubsub'). The prefix for the collection names that would be created by this module.
  • separator - (default=':'). The separator between ns and the pubsub related namespaces.
  • collectionNameGenerator - (Optional function). You can implement your own naming scheme.
  • dao - (Optional object). You can implement your own MongoDB Data Access Object. The default naming scheme is: '%ns%%separator%%CHANNEL_NAME%.
  • writeStreamFactory - (Optional function). Returns a Writeable Stream that stores events in MongoDB. You can implement your own storage scheme.
  • readStreamFactory - (Optional function). Returns a readable stream with events being published. You can implement your own retrieval strategy.

Events:

  • error - All errors would be redirected to the main instance of MubSub.

var tstChannel = pubsub.channel(channelName);

Returns a Channel instance from brocast.

Args:

  • channelName - The name of the channel.

Events:

  • newSubscriber - When someone subscribes to this channel it emits the Subscriber object.

tstChannel.publish(name, message)

Publishes a new message to the channel.

Args:

  • name - The name of the event to publish.
  • message - The message to publish. It can be String, Array or Json object.

var subscriber = tstChannel.subscribe(eventName, opts);

Returns an instance of brocast subscriber object, which is a Readable stream.

Args:

  • eventName - The name of the event to subscribe to.
  • opts - Object with options.

Options:

  • start - The last eventId that you dispatched. If undefined, it'll subscribe only to new events. If the value is 0 it will subscribe to all events available in the channel.

Events:

  • unsubscribe - This event is emitted if you call the unsubscribe method.
  • data - The events that are being dispatched.

Event struct

A dispatched event looks like this:

{
  _id: 542bf84fa51ffd383cea96ff,
  name: 'test event',
  channel: 'tstChannel',
  timestamp: 1412167759481,
  msg: 386.00159669294953
}

subscriber.unsubscribe();

Unsubscribe from receiving events.

Example:

var MongoClient = require("mongodb").MongoClient,
  MubSub = require('../lib/mubsub');

var dbConfig = {
    "uri": "mongodb://192.168.111.222:27017/mubsub",
    "options": {
    "server": {
      "socketOptions": {
        "keepAlive": 1
      }
    }
  }
}

MongoClient.connect(dbConfig.uri, dbConfig.options, function (err, db) {
  var client = new MubSub(db);
  var channel = client.channel('tstChannel');

  var sub = channel.subscribe('test event').on('data', function (event) {console.log('received', event)})

  channel.publish('test event', Math.random()*1000);
});

Future Plans

  • Standardize the Event Struct and decouple it from Mongo. i.e. id instead of _id.
  • When subscribing, add another option lastId to act as an alias for start
  • Implement a retrial mechanism in case there is a DB failure. (Probabbly in a separate module)
  • Implement balanced event insertion. Right now, the events are inserted as they arrive, without back pressure concern.
  • Implement bulk event insertion using MongoDB's Bulk insertion feature.
  • Not directly related to this module, but it should be implemented for the browser as well.
  • Find a better name.

install

With npm do:

npm install brocast

license

MIT