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 🙏

© 2026 – Pkg Stats / Ryan Hefner

socket-router

v1.5.1

Published

Simple Socket Routing

Readme

Socket Router

Easily route communication over socket connection.
Module supports both standard socket connections and WebSockets.
Module also allows routes to optionally reply to messages received from clients. Support for es6 Promises

Install

$ npm install socket-router --save
$ bower install socket-router --save

API

// TODO: Better docs

Server

server.listen ( socket )

server.route ( path, callback( data, reply ) )

server.send ( socket, path, callback( data, reply ) )

Client

server.listen ( socket )

server.route ( path, callback( data, reply ) )

server.send ( path, callback( data, reply ) )

Reply ( data )

Usage

Example 1: Node Server to Node Server Communication

In this example uses json-socket to convert the stream to JSON.

Host Server

var net = require('net');
var JsonSocket = require('json-socket');
var SocketRouter = require('socket-router');

var server = new SocketRouter.Server();
var socketServer = net.createServer();

socketServer.listen(3000);

JsonSocket.prototype.send = JsonSocket.prototype.sendMessage; //TODO: Hack :/

socketServer.on('connection', function (socket) {
    server.listen(new JsonSocket(socket));
});

server.route('addition', function(data, reply) {
    console.log(data);
    reply({ result: data.a + data.b });
});

Client Server

var net = require('net');
var JsonSocket = require('json-socket');
var SocketRouter = require('socket-router');

var socketServer = new JsonSocket(new net.Socket());
socketServer.connect(3000, '127.0.0.1');

JsonSocket.prototype.send = JsonSocket.prototype.sendMessage; //TODO: Hack :/

var server = new SocketRouter.Client(socketServer);

server.send('addition', { a: 4, b: 8 }, function(err, data) {
    if(err) throw err;
    console.log("Answer:", data.result);
});

Example 2: Node Server to Browser Client Communication

In this example using our own basic JSON Socket Wrapper to convert the stream to JSON and back to stream again.

Server

// Basic JSON Socket Wrapper
var EventEmitter = require('events').EventEmitter;
function JSONSocketWrapper(ws) {
    EventEmitter.call(this);
    this._ws = ws;
    ws.on('message', this.msg.bind(this));
}
JSONSocketWrapper.prototype = Object.create(EventEmitter.prototype);
JSONSocketWrapper.prototype.msg = function (data) {
    this.emit('message', JSON.parse(data));
};
JSONSocketWrapper.prototype.send = function (data) {
    this._ws.send(JSON.stringify(data));
};
// End Basic Socket Wrapper

var WebSocketServer = require('ws');

var wss = new WebSocketServer.Server({ port: 3000 });
var server = new SocketRouting.Server();

// Catch All routes
server.route('*', function() {
    console.log("Oh.");
});

server.route('addition', function(data, reply) {
    console.log(data);
    reply({ result: data.a + data.b });
});

wss.on('connection', function connection (ws) {
    server.listen(new JSONSocketWrapper(ws));
});

Client

// Basic JSON Socket Wrapper
function JSONSocketWrapper(ws) {
    EventEmitter.call(this);
    this._ws = ws;
    ws.addEventListener('message', this.msg.bind(this));
}
JSONSocketWrapper.prototype = Object.create(EventEmitter.prototype);
JSONSocketWrapper.prototype.msg = function (e) {
    this.emit('message', JSON.parse(e.data));
};
JSONSocketWrapper.prototype.send = function (data) {
    this._ws.send(JSON.stringify(data));
};
// End Basic Socket Wrapper

var ws = new WebSocket('ws://localhost:3000');
var server = new SocketRouting.Client(new JSONSocketWrapper(ws));

server.send('addition', { a: 4, b: 8 }, function(err, data) {
    if(err) throw err;
    console.log("Answer:", data.result);
});