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

bellhop

v0.4.1

Published

Pubsub and RPC streams for node.js

Downloads

26

Readme

Description

A node.js module that exposes streams for doing Pubsub and RPC.

Serialization of types unsupported by JSON is also an option and is enabled by default. It should also be noted that properties of "non-plain objects" (e.g. Dates, Functions, Errors, RegExps) are not checked for needed serialization.

Requirements

Install

npm install bellhop

Examples

  • Simple RPC (no intermediate medium/stream):
var RPC = require('bellhop').RPC;

var RPC_Server = new RPC(),
    RPC_Client = new RPC();

RPC_Server.add(function add(a, b) {
  var cb = arguments[arguments.length - 1];
  cb && cb(a + b);
});
RPC_Server.add(function serverDate() {
  var cb = arguments[arguments.length - 1];
  cb && cb(new Date());
});
RPC_Server.add(function customCalc(fn) {
  var cb = arguments[arguments.length - 1];
  cb && cb(fn());
});


var add = RPC_Client.makeRemoteFn('add'),
    serverDate = RPC_Client.makeRemoteFn('serverDate'),
    customCalc = RPC_Client.makeRemoteFn('customCalc');

add(5, 5, function(result) {
  console.log('add() result = ' + result);
});
serverDate(function(date) {
  console.log('serverDate() date UNIX timestamp: ' + date.getTime());
});
customCalc(function() {
  console.log('Look at me, I am running on the server!');
}, function() {
  console.log('customCalc() Finished executing function on server');
});


RPC_Client.pipe(RPC_Server).pipe(RPC_Client);


// Example output:
//
// add() result = 10
// serverDate() date UNIX timestamp: 1383242166000
// Look at me, I am running on the server!
// customCalc() Finished executing function on server
  • Simple Pubsub (no intermediate medium/stream):
var Pubsub = require('bellhop').Pubsub;

var Pubsub1 = new Pubsub(),
    Pubsub2 = new Pubsub();

Pubsub1.events.on('today', function(date) {
  console.log('Today is: ' + date);
});

Pubsub2.events.emit('today', new Date());

Pubsub1.pipe(Pubsub2).pipe(Pubsub1);


// Example output:
//
// Today is: Thu Oct 31 2013 14:02:00 GMT-0400 (Eastern Daylight Time)

API

All types are Duplex streams.

RPC methods

  • (constructor)([< object >options]) - Creates and returns a new RPC instance with the following valid options:

    • serialize - boolean - Manually serialize objects that JSON does not support (well)? (Default: true).

    • highWaterMark - integer - High water mark to use for this stream (Default: Duplex stream default).

    • Additionally options is passed to the underlying Xfer instance, allowing for configuration of Xfer too if needed (not common).

  • makeRemoteFn(< string >remoteFuncName) - function - Returns a function that can be used when calling a particular remote function. This makes things easier than using send() manually. The return value of the returned function is similar to that of Writable.write() and indicates if the high water mark has been reached.

  • send([< mixed >arg1, ..., < mixed >argn, ]< string >remoteFuncName[, < function >callback]) - (boolean) - Calls the function identified by remoteFuncName (with optional arguments). The return value is similar to that of Writable.write() and indicates if the high water mark has been reached.

  • add(< function >method[, < string >methodName]) - (void) - Adds a function that can be called by others. methodName is optional if method is a named function, however you can always override the name with methodName. The last argument passed to the function is the callback to call in case the other side requested a response. The return value of the callback is similar to that of Writable.write() and indicates if the high water mark has been reached.

  • remove([< function >method][, < string >methodName]) - (void) - Removes a function previously added via add(). The removal process first checks methodName, then method for a name, and if those two are not set then all instances of that function are removed.

Pubsub properties

  • events - EventEmitter - This is the event emitter object used to emit events to others and to receive events from others.

Pubsub methods

  • (constructor)([< object >options]) - Creates and returns a new Pubsub instance with the following valid options:

    • serialize - boolean - Manually serialize objects that JSON does not support (well)? (Default: true).

    • highWaterMark - integer - High water mark to use for this stream (Default: Duplex stream default).

    • Additionally options is passed to the underlying Xfer instance, allowing for configuration of Xfer too if needed (not common).