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

socketsaurus

v0.1.2

Published

Node module for Socketsaurus

Downloads

6

Readme

Socketsaurus Circle CI

Socketsaurus exposes changes in your Mongoose collections, via sockets.

NPM

npm install socketsaurus --save

Usage

Call Socketsaurus with your config, and it will return you a function that you can use to expose collections.

// io = require('socket.io')(server);

var socketsaurus = require('socketsaurus');

var expose = socketsaurus(io, {
  // required
  oplogUrl: 'mongodb://<user>:<password>@candidate.35.mongolayer.com:10491,candidate.34.mongolayer.com:10493/local?authSource=oplog_test',
  // required
  db: 'dev'
});

Then, you can expose any Mongoose collection.

var mongoose = require('mongoose');

var schema = mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  created: {
    type: Date,
    default: Date.now
  },
  updated: Date
});

schema.pre('save', function(next) {
  this.updated = Date.now();
  next();
});

var Person = mongoose.model('people', schema);

expose(Person);

Events

Each collection exposed will emit the following events:

  • created
  • removed
  • modified
  • modification

See docs on the Socketsaurus JS Client for examples on listening to these events.

Events are emitted within namespace, per collection. If exposing a Person model, that is stored in the People collection in the database, then any event will be emitted to the People namespace.

Clients can listen to specific attributes of documents by sending an event to the server.

Server

expose(Person);

Client

// joining people namespace
var socket = io('localhost:3000/states');
// only want to listen to a specific attribute event
socket.emit('child', 'cities.population');
// will only fire events within `states.cities.population`
socket.on('modified', function(doc) {});

Additional Options

prep

Each document passes through prep before being sent to the client.

For example, if you wanted to remove password from any document before emitting, you could:

var expose = socketsaurus(io, {
  oplogUrl: 'mongodb://<user>:<password>@candidate.35.mongolayer.com:10491,candidate.34.mongolayer.com:10493/local?authSource=oplog_test',
  db: 'dev',
  prep: function(doc) {
    doc = doc.toJSON();
    delete doc.password;
    return doc;
  }
});

auth

Each connection to a socket namespace passes through auth to verify it is authorized. The default auth just passes it through, regardless.

var expose = socketsaurus(io, {
  oplogUrl: 'mongodb://<user>:<password>@candidate.35.mongolayer.com:10491,candidate.34.mongolayer.com:10493/local?authSource=oplog_test',
  db: 'dev',
  auth: checkAuth
});

function checkAuth(io) {
  io.use(function(socket, next) {
    var auth = socket.handshake.query.token;

    if (!token) {
      return next(new Error('invalid permissions'));
    }

    // further checks on token

    next();
  });

  return io;
};

namespace

If you want to customize the namespace labels, you can do so by overriding namespace. By default, all namespaces are the database collection names.

var expose = socketsaurus(io, {
  oplogUrl: 'mongodb://<user>:<password>@candidate.35.mongolayer.com:10491,candidate.34.mongolayer.com:10493/local?authSource=oplog_test',
  db: 'dev',
  namespace: function(name) {
    // now events for the collection with name 'people' will be emitted under the namespace 'socketsaurus_people'
    return 'socketsaurus_' + name;
  }
});

Lint

To lint the code, first npm install the dev dependencies, and then run grunt lint.