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

socketbox

v0.8.2

Published

Socketbox is real time socket layer framework inspired by express

Readme

Socketbox

npm version Build Status Coverage Status

Socketbox is real time socket layer framework inspired by express.You can simulate socket messages like as restful request, build router system according to a specific protocol and write middleware to this routers.

import Socketbox from 'socketbox';
// First create your own socket server.
const ws = new WebSocket.Server( { port : 8080 } );

// you give socket server instance to socketbox
const app = new Socketbox();

app.createServer( ws );

//you create router
const router = Socketbox.Router();

//you activate router on this websocket server.
app.use( '/', router );

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 7 or higher is required.

$ npm install socketbox

Supported socket types

  • Websocket

Socketbox opts

const app = new Socketbox(opts);

Events | Description -----------------|------------ ping | (boolean) ping-pong activate, server send ping message automatic pingTimeout | (number) seconds, ping sending timeout

Basic text message based ping-pong

Your client listen ping text message and reply to this message
!! Strongly recommend set clientTracking to false in websocket opts
// client connect to server
var wsClient = new WebSocket('ws://localhost:8080', opts);

// example Web browser websocket
wsClient.onmessage = function(message){ 
  if(message.data === 'ping') {
    wsClient.send('pong');
  }
}
If your client dont reply ping message, server know client is down and clear client data on system.

Socketbox client events

app.on(<events>, (client) => {
  console.log(`${client.ip} is connected`);
});

Events | Description -----------------|------------ connected | Emitted client connected disconnected | Emitted client disconnected

Event callback give one parameter, it is client object.

Client sessions

Your each client have own session object.You can edit session add and remove directly. You can write data to client session.

// example:
// you validate client and returned user object.You
// can put user to client session.You can access session in each request 
// session everytime stored until client disconnect
router.register( '/validate/token', ( req, res ) => {
  req.session.user = validate(req.body.token); //use session.user on each request
} );

Socket message protocol (Client message format)

You need to adjust some rules while send message to this socket server from client. Your socket message architecture below;

{
  url: 'ws://omer.com/message/write', // hostname and protocol is required current version
  body: {
    to: 'x',
    from: 'y',
    message: 'hi!'
  }
}

Property | Description ---------|------------ url | ( required ) your defined url on backend server body | ( optional ) if you want put data to your message

you can put query your url.
{
  url: 'ws://omer.com/message/get?messageId=11993'
}

Router

router.register( '/message/write', ( req, res ) => {
  res.send( { statusCode : 200 } );
} );

you can use params while register route path.

router.register( '/message/write/:userid', ( req, res ) => {
  /**
   * Access to params
   * req.params.userid
   * 
   * If you have query in request;
   * req.query.[<your query name>]
   */
  
  res.send( { statusCode : 200 } );
} );

Middleware

You can add property to request object on middleware.

const mid1 = ( req, res, next ) => {
  req.user = {};
  req.user.username = 'dmomer';
  next();
};

router.register( '/profile', mid1, ( req, res ) => {
  res.send( req.user );
} );

Cache

You can access socketbox cache but recommend only use the following methods. Cache class is static class and read-only.Clients is stored in a Map object.


// return everytime same cache class.
const cache = Socketbox.Cache();

Method: cache.clients()

  • Returns: All clients in a Array.

don't take parameters.

Method: cache.filter(key)

  • key <string> | <Function> You will be passed to key string or direct working filter function.
  • Returns: All clients in a Array.

Following is two differenct parameter example:


// we have map , which is [['1', {name: 'omer'}], ['2', {name: 'demircan'}]]

// use string key
cache.filter('1'); // return only values [{name: 'omer'}]

// use own filter function;
// following function filter objects, which contains name key is 'omer'
cache.filter((item) => {
  // item[1] is value.
  return item[1].name === 'demircan'
}) // return [{name: 'demircan'}];

Channels

You can bind clients to channels.You can send message to channel if you want.

Join to channel

Method: join( cname )

  • cname <String> channel name for join
// if channel is not exist, method create channel.
client.join('channel1');

// ex:
// you can listen join router
router.register('/join?room=223', (req, res) => {
  res.join(req.query.room);
});

Send message to channel

Method: sendTo(message, cname)

  • message <String> message content
  • cname <String> channel name to go
res.sendTo({message: x}, 'channel1');

Leave from channel

Request

You can use some request properties.

req.body; //you can access message body

// if url is /a/b?keyword=omer
// you can access req.query.keyword (value is omer)
req.query; //this is object

// if url is /a/b/:name
// you can access req.params.name (value is any) 
req.params; //this is object

// if your url is wss://omer.com/p/a/t/h?query=string#hash
req.protocol; //wss:
req.host; //omer.com
req.hostname; //omer.com
req.port; // null
req.path; // /p/a/t/h?query=string
req.pathname // /p/a/t/h
req.href; // wss://omer.com/p/a/t/h?query=string#hash
req.query; // 'query=string'
req.hash; // #hash

Response

Actually response object is client reference.

You can put string, json or buffer to message.

// you can send Object or raw string

res.send({name: 'omer'});

res.send(new Buffer('omer'));

Leave from room

Method: leave( cname )

  • cname <String> channel name for leave

If client is leaved, return true otherwise return false.

// res is client reference on all request, you can access to client object using res.

res.leave('room1');