Latest version : 1.3.6

 Last updated on 2015-07-15T19:54:19.762Z

 Keywords :, websocket, 39f, 39-framework, framework, realtime, socket, io, tcp, events

 Downloads :

  • 142 in Last Month

 Links :


No examples found for this package


Handy wrapper, which adds to handlers' chains and middlewares.


tl;dr?: To add to middlewares and handlers' chains and to make event handling more comfortable than in stock is a genially perfect tool, but, for a lot of tasks, it is a bit low-level and complicated, event handling is unpleasant, there is no way to specify handlers chain or mask-based handler (middleware). solves all this problems for you. It's fully compatible with But, if you want to use features, provided by, you shouldn't write server code in classic socket-io style.


Please, refer to documentation.

To prevent undefined behaviour you shouldn't call on('connection') on namespace and on('disconnect') and on('some event') on socket.

Instead, you should call Namespace#use, Namespace#route, Socket#use and Socket#route.

  1. use(handler:Function) - adds handler to the middlewares chain.
  2. use(eventNamePattern:RegExp, handler:Function) - adds handler to the middlewares chain for events, which satisfies given pattern.
  3. route(eventName:string, handler:Function|Object) - adds handler for event with event name. Handler may be an object, containing handler functions, if it is, route creates handlers for every function, and event names are constructed like this: eventName:functionField.

Calling this functions for Namespace, execute actions for every new connections, calling them for Socket, affects only that socket.

Note, that you should call use before any route.

Note, that you can create chains of handlers calling route multiple times for one event.

Event object is passed to handler as first argument. Here is it's structure:

 * @param {!Object} event
 * @param {*} Received data
 * @param {!Object} event.socket Legacy object, associated with the event
 * @param {!string} Event name
 * @param {!string} event.namePrefix Part of event name before ':'
 * @param {!function(*)} event.respond Function to send ack response to client
 * @param {!Function} event.emit Just a shorthand for event.socket.emit

When event received, first handler in chain called. To call next handler in chain, you should call next() function manually (passed to handler as second parameter, if here is next handler in chain).

~~In addition, this module adds Namespace#leave and Namespace#join methods, which works perfectly similar to stock Socket#leave and Socket#use, but for all sockets in the namespace. It may be used to add sockets in some room into another room.~~ (Doesn't work with 1.0.x, needs patching)


var io = require('');

var app = new io();

app.sockets.use(function(signal, next) {

app.sockets.use(function(signal, next) {
  var authorized = checkAuth();
  if (authorized) {
  } else {
    signal.respond('Authorization required');

app.sockets.route('123', function(signal) {
  signal.respond({1: 23});

app.of('/some').use(function(signal, next) {
  console.log('namespace some');

app.of('/some').route('hi', function(signal) {
  signal.respond('you too');


Notice about logger

By default logger uses console.error method to log errors and console.log to log data, sent with response (the last logged only if node launched in development mode (NODE_ENV=development)).

You may specify logger (object with warn and debug methods (winston perfectly fits)) by passing it as last argument to Server constructor.


$ npm install

Author: Yurij Mikhalevich