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

mongoose_rpc

v0.1.0

Published

A mongoose plugin which enables RPC with tailable querys and capped collections.

Downloads

4

Readme

Mongoose RPC

Enables easy to use RPC over mongoose/mongodb.

I made this because I did not want to open another socket or add another technology to my project as I already had a connection to mongodb using mongoose.

It works by using a tailable cursor query and streaming the records that it matches to a function which processes them further. Functions can be exposed for use by RPC.

If you are looking for something faster use UDP with some sort of message queue. But if you don't want to add more to your stack and already have mongoose/mongodb then this seems pretty decent.

The Schema is:

from: String,
to:   { type: String, default: '*' },
ttl:  { type: Date, default: function() { Date.now + 1000 } }, // Messages will be relevent 1000 ms by default
fn: String,
args: Array,
cb: String

But you should just use rpc.send('function name',[args],to, callback etc)

Installation

npm install mongoose_rpc

Note: I have only tested this with mongoose version 3.8.23

Usage

See go.js for more examples.

var MongooseRPC = "require('mongoose_rpc')"

// ... Connect to mongoose etc

var collection = 'rpc'
var name = 'Test' // It is a good idea to make name unique for each process.
                  // If a process is an instance/clone of another you might do something like Instance.1 Instance.2 etc.
                  // Name should not have spaces or , or *
var rpc = new MongooseRPC(mongoose, collection, name)


// Expose a function
// There are a few ways to expose a function lets look at some.

// Method 1 Named function inline
rpc.expose(function myFunction(){})

// Method 2 (Technically Method 1 still)
function myFunction(){}
rpc.expose(myFunction)

// Method 3 using a name supplied as a string should allow you to have functions exposed with names like 01234 not that its recommended.
rpc.expose('myFunction', function() {
	
})

// Oh and start the rpc stream you probably want to do this after mongoose connects.
rpc.start()

// Sending a request to every rpc listening it uses * as the to
// no arguments
rpc.send('myFunction')

// arguments
rpc.send('myFunction', [1,2,3])

// no arguments callback
rpc.send('myFunction', function(err,result){ 
   if (err) {
     console.error(err)
     return
   }

   console.log('Result is: '+result)
})

// Choosing who a request goes to such as Instance1.1
rpc.send('myFunction',[0], 'Instance1.1')

// All instances of 1
rpc.send('myFunction',[0], 'Instance1.*')

// All Instances
rpc.send('myFunction',[0], 'I.*')

// Multi Levels of .
rpc.send('myFunction',[0], 'Test.NewZealand')

// Amercia and Europe etc seperate multiple receipents with a comma and space ', '.
rpc.send('myFunction',[0], 'Amercia, Europe')


// Expose a function to do something and return
function sum(a,b) {
	return a+b
}

rpc.expose(sum)

// Ask a process with name of Calculator to do a sum function and give you a result
var args = [1,4]
rpc.send('sum',args,'Calculator', function(err, result){
if (err) {
  console.error(err)
  return
}
	
console.log('The sum of '+args+' is '+result)
})

// Trigger error on exposed function
function asdf() {
  throw new Error('Not the bees!');
}

// Or another way
function asdf2() {
  this.error('Poop')
}


// On an exposed function say you will take longer than is set to do a function
// May be usefull if generating a lot of data or doing async requests etc.
// This will ensure the thing that asked you to execute will keep its callback around at least that long.
function e() {
	this.expandTTL(60000) // Ask for an extra minute
}

// Saying you had an error in your exposed function to the thing asking you to execute it
function e(a) {
	if (!Array.isArray(a) || a.length < 4) {
	   this.error('Expecting argument a to be an array and have at least a length of 4')
       return
    }

    // do something with a eg max
    return Math.max.apply(null, a)
}

// Returning a result from an exposed function more than once or in a closure

function e(a) {
	
	var request = this;
	setTimeout(function() {
        request.result(10)
	},5)
    
}


// Sending a request with an increased ttl (TTL means time to live by the way it is roughly how long to keep a callback around if set and how long other clients should accept messages.)

rpc.send('doSomething', [1,2,3], function(err,data){  console.log(data) }, 3600000) // 1 hour

// If a client starts up it will accept messages that were sent out for it prior to its startup if their ttl >= Date.now()

// Ping
rpc.ping()

// Ping named
rpc.ping('Test')

// ping using latency in function
rpc.ping('*', function(who, latency){ console.log('Ping to '+who+' took '+latency+' ms.') })

You can also error inside your function handler with an exception throw or return this.error('A problem happened!')

Dependencies

  • node-uuid
  • mongoose

Issues

Have an issue to report? Just report it on github here: New Issue

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

History

Credits

Liam Mitchell

License

MIT