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

messenger

v0.0.9

Published

A json communication layer for node.js

Downloads

922

Readme

Messenger.js - Fast Node.js Communication Library

Installation

npm install messenger

What is Messenger.js?

Messenger.js is a library that makes network communication via JSON dead simple and insanely fast!

Example:

var messenger = require('messenger');

client = messenger.createSpeaker(8000);
server = messenger.createListener(8000);

server.on('give it to me', function(message, data){
  message.reply({'you':'got it'})
});

setInterval(function(){
  client.request('give it to me', {hello:'world'}, function(data){
    console.log(data);
  });
}, 1000);

Output:

> {'you':'got it'}
> {'you':'got it'}
> ...etc...

Features

Messenger.js is very flexible and can handle everything you need.

  • Supports Request / Reply Communication using round robin
  • Supports Publish / Subscribe (fanout) Communication
  • Supports Fire and Forget Communication
  • Supports middleware plugin for messenger Listeners (servers)
  • Extremely fast (disables TCP Nagle's algorithm)
  • Fault tolerant: clients will reconnect to servers even if server goes down and comes back later
  • Elegant API
  • Easily involves multiple servers
  • Zero dependencies on other libraries

Pub Sub Example - .shout

Example

var messenger = require('messenger');

// here we have 4 servers listening on 4 different ports
var server1 = messenger.createListener(8001);
var server2 = messenger.createListener(8002);
var server3 = messenger.createListener(8003);
var server4 = messenger.createListener('127.0.0.1:8004');

server1.on('a message came', function(m, data){
  // note that m.data and data are equivalent
  console.log('server 1 got data', data);
});

server2.on('a message came', function(m, data){
  console.log('server 2 got data', data);
});

server3.on('a message came', function(m, data){
  console.log('server 3 got data', data);
});

server4.on('a message came', function(m, data){
  console.log('server 4 got data', data);
});

// a client that can be used to emit to all the servers
var client = messenger.createSpeaker(8001, 8002, 8003, 8004);

setInterval(function(){
  client.shout('a message came', {some: data});
}, 1000);

Output

> server 1 got some data
> server 2 got some data
> server 3 got some data
> server 4 got some data
> ... repeat ....

Multi-Server Request Reply - .request

Messenger uses round robin to pick a server to send data to.

Example

var messenger = require('messenger');

// here we have 4 servers listening on 4 different ports
var server1 = messenger.createListener(8001);
var server2 = messenger.createListener(8002);
var server3 = messenger.createListener(8003);
var server4 = messenger.createListener('127.0.0.1:8004');

server1.on('a message came', function(m, data){
  m.reply({greetings:'server 1 got some data'});
});

server2.on('a message came', function(){
  m.reply({greetings:'server 2 got some data'});
});

server3.on('a message came', function(){
  m.reply({greetings:'server 3 got some data'});
});

server4.on('a message came', function(){
  m.reply({greetings:'server 4 got some data'});
});

// a client that can be used to emit to all the servers
var client = messenger.createSpeaker(8001, 8002, 8003, 8004);

setTimeout(function(){
  // request here instead of shout to only access 1 server
  client.request('a message came', {some: 'data'}, function(data){
    console.log(data.greetings);
  });
}, 2000);

Output

> server 1 got some data

Fire and Forget with Multiple Servers - .send

Again, using round robin to pick a server to send data to.

Example

var messenger = require('messenger');

// here we have 4 servers listening on 4 different ports
var server1 = messenger.createListener(8001);
var server2 = messenger.createListener(8002);
var server3 = messenger.createListener(8003);
var server4 = messenger.createListener('127.0.0.1:8004');

server1.on('a message came', function(m, data){
  console.log(data);
});

server2.on('a message came', function(){
  console.log(data);
});

server3.on('a message came', function(){
  console.log(data);
});

server4.on('a message came', function(){
  console.log(data);
});

// a client that can be used to emit to all the servers
var client = messenger.createSpeaker(8001, 8002, 8003, 8004);

setTimeout(function(){
  // use send instead of reply
  client.send('a message came', {some: 'data'});
}, 2000);

Output

// via one of the 4 servers
> {'some': 'data'} 

Plugin (Middleware) Example

Take a look at the function authRequired.
You may augment the value of data by changing m.data (see Example).

Example

var messenger = require('messenger');

var server = messenger.createListener(8000);
var client = messenger.createSpeaker(8000);

function authRequired(m, data) {
  if (data.authorized) {
    m.data.coolness = 10; // modify m.data if you want it passed, not data
    m.next(); // continuation passing
    return;
  }
  m.reply({error:'not authorized'});
}

server.on('protected request', authRequired, function(m, data){
  m.reply({you:'got past security with a coolness factor of ' + data.coolness})
});

var auth = false;
setInterval(function(){
  
  client.request('protected request', {authorized:auth}, function(data){
    console.log(data);
  })
  
  if (auth === false) {
    auth = true;
  } else {
    auth = false;
  }
}, 2000);

Output

> {error: 'not authorized'}
> {you:'got past security'}
> {error: 'not authorized'}
> {you:'got past security'}
> ... etc ...