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

message-center

v0.1.10

Published

A common event based message center that can handle invoke/event

Downloads

31

Readme

MessageCenter is a transfer layer agnostic RPC protocol base on json. It's a more human friendly protocol than machine friendly one.

Feature:

  1. transparent javascript type serialize (Buffer,Date)
  2. support reliable RPC call and not reliable event dispatch.
  3. support nodejs streaming with easy to use API.

To use this protocol the underlying transfer layer should meet these requirements.

  1. Can send block of data.
  2. Each block of data should either be completely recieved without any mistake or completely droped.

Each block of data is a JSON with some special field, I will describe it latter and let us see some example of using node-message-center with ws - a great websocket implementation in nodejs.

Install and test

# install
npm install message-center
# test
# you will need mocha to run the test
# you can get mocha with (sudo) npm install -g mocha
cd node_modules/message-center
npm test

Example

WebSocket = require("ws");
WebSocketServer = WebSocket.Server;
MessageCenter = require("message-center");

mcClientSide = new MessageCenter();
mcServerSide = new MessageCenter();

server = new WebSocketServer({hostname:"localhost",port:12345})
server.on("connection",function(conn){
    mcServerSide.setConnection(conn);
    mcServerSide.invoke("whoAreYou",{key:"Any data",time:new Date(),buffer:new Buffer("hehe")},function(err,res){
        console.log(res);
    })
})

connection = new WebSocket("ws://localhost:12345/");
connection.on("open",function(){
    mcClientSide.setConnection(connection);
})
connection.on("close",function(){
    mcClientSide.unsetConnection();
})

mcClientSide.registerApi("whoAreYou",function(data,callback){
    console.log("Date is reserved",data.time);
    console.log("Buffer is reserved",data.buffer);
    console.log("It's time to ask who YOU are")
    callback(null,"I'm client and I got your data "+JSON.stringify(data));
    mcClientSide.invoke("areYouServer",null,function(err,data){
        console.log("get response from server:",data);
    })
})
mcServerSide.registerApi("areYouServer",function(data,callback){
    callback(null,"I'm server and I got your data "+JSON.stringify(data));
})

MessageCenter has a default timeout of 60 * 1000 ms, if you don`t like it you can set it manually.

var invoke = mcClientSide.invoke("delayReturn",5000,function(err,data){
    console.error(err,data);
    console.assert(err.message === "timeout");
})
invoke.timeout(3000);
//or set for all invokes
mcClientSide.timeout = 3000;
//every request has a 3000 timeout.

MessageCenter also support event dispatching without promising.

mcServerSide.fireEvent("archive/update",{content:"Lorem Ipsum"})
//Note here we have a "event/" prefix so we don't always conflict with local events by accidents.
mcClientSide.on("event/archive/update",function(data){
    console.log("get data",data,"from event archive/update");
})

Streaming

mcServerSide.registerApi("anyApi",function(_,callback){
    var stream = mcServerSide.createStream();
    // transfer just like normal object
    callback(null,stream)
    stream.write("can you get me?");
    stream.end("bye!");
})
mcClientSide.invoke("anyApi",{},function(err,stream){
    var result = [];
    stream.on("data",function(data){
        result.push(data);
    })
    stream.on("end",function(){
        console.log("stream end");
        console.log(result.join("\n"));
    });
    return;
})

General error handle designs:

  1. Any error caused by underlying connection will just fail silently. (say throw error in send)
  2. Any error caused by broken data will emit an error event. (likely broken connection or some violation, you'd better unsetConnection immediately)
  3. Other exception like broken paramaters will just throw. (likely due to unmatched MessageCenter version)

Notes: