Stats

 Latest version : 1.3.6

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

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

 Downloads :

  • 82 in Last Month

 Links :

 Examples

¯\_(ツ)_/¯
No examples found for this package

 Readme

39f-socket.io

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

Motivation

tl;dr?: To add to socket.io middlewares and handlers' chains and to make event handling more comfortable than in stock socket.io.

socket.io 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).

39f-socket.io solves all this problems for you. It's fully compatible with socket.io-client. But, if you want to use features, provided by 39f-socket.io, you shouldn't write server code in classic socket-io style.

Usage

Please, refer to socket.io 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 {*} event.data Received data
 * @param {!Object} event.socket Legacy socket.io object, associated with the event
 * @param {!string} event.name 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 socket.io 1.0.x, needs patching)

Example

var io = require('39f-socket.io');

var app = new io();

app.sockets.use(function(signal, next) {
  console.log('use');
  next();
});

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

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

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

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

app.listen(8080);

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.

Installation

$ npm install 39f-socket.io

Author: Yurij Mikhalevich

 Comments