hapi-io

0.2.1 • Public • Published

hapi-io

Greenkeeper badge npm Build Status Dependency Status devDependency Status

Awesome socket.io plugin for hapi (inspired by express.oi and express.io).

Table of Contents

Installation and Configuration

npm install hapi-io --save
server.register({
  register: require('hapi-io'),
  options: {
    ...
  }
});
Options
  • connectionLabel (optional)
  • namespaces (optional) - an array of strings representing namespaces. Namespaces must always begin with a slash '/' (e.g. '/mynamespace'. The default '/' namespace is always available irrespective if explicitly specified, and will be the only namespace available to routes if this option is not set upon plugin initialization.
  • socketio (optional) - an object which is passed through to socket.io
  • auth (optional) - authorization configuration. Socket.io connections will be refused if they fail authorization. If this option is omitted: all socket.io connections will be accepted, but route level authorization will still be enforced. Value can be:
    • a string with the name of an authentication strategy registered with server.auth.strategy().
    • an object with:
      • strategies - a string array of strategy names in order they should be attempted. If only one strategy is used, strategy can be used instead with the single string value.

Raw access to socket.io

You can get raw access to the socket.io server as follows:

exports.register = function(server, options, next) {
 
  var io = server.plugins['hapi-io'].io;
 
};

Forward events to hapi routes

Perfect for exposing HTTP API endpoints over websockets!

socket.io events can be mapped to hapi routes; reusing the same authentication, validation, plugins and handler logic.

Example
Server
exports.register = function(server, options, next) {
 
  server.route([
 
    {
      method: 'GET',
      path: '/users/{id}',
      config: {
        plugins: {
          'hapi-io': 'get-user'
        }
      },
      handler: function(request, reply) {
        db.users.get(request.params.id, function(err, user) {
          reply(err, user);
        });
      }
    },
 
    {
      method: 'POST',
      path: '/users',
      config: {
        plugins: {
          'hapi-io': {
            event: 'create-user',
            mapping: {
              headers: ['accept'],
              query: ['returnType']
            }
          }
        }
      },
      handler: function(request, reply) {
        db.users.create(request.payload, function(err, user) {
          if (err) {
            return reply(err).code(201);
          }
 
          if (request.headers.accept === 'application/hal+json') {
            addMeta(user);
          }
 
          if (request.query.returnType !== 'full') {
            delete user.favoriteColor;
          }
 
          reply(err, user);
        });
      }
    },
 
    // '/admin' namespace
    {
      method: 'GET',
      path: '/users/{id}',
      config: {
        plugins: {
          'hapi-io': {
            event: 'create-user',
            namespace: '/admin'
          }
        }
      },
      handler: function(request, reply) {
        db.adminUsers.get(request.params.id, function(err, user) {
          reply(err, user);
        });
      }
     },
 
  ]);
};
Client
var socket = io();
 
socket.emit('get-user', { id: 'sibartlett'}, function(res) {
  // res is the result from the hapi route
});
 
socket.emit('create-user', {
  name: 'Bill Smith',
  email: 'blsmith@smithswidgets.com',
  location: 'remote',
  favoriteColor: 'green',
  returnType: 'full'
}, function (res) {
  // do something with new user
});
 
// '/admin' namespace
var socketA = io('/admin');
 
socketA.emit('get-admin-user', { id: 'mmemon'}, function(res) {
  // res is the result from the hapi route
});
How it works

Each time an event is received, a fake HTTP request is created and injected into the hapi server.

The fake HTTP request is constructed as follows:

  1. The headers and querystring parameters from the socket.io handshake are added to the fake request.

This allows you to use the route's auth stategy - to authenticate the socket.io event.

  1. Each field in the event payload is mapped to one of the following hapi param types: headers, path, query or payload. The mapping is determined on a per field basis:

  2. If the field is a parameter in the route's path, it's mapped as a path parameter.

  3. If the hapi-io config is an object and has a mapping property, then the field is checked against the mapping. Allowed mappings are headers, query, and payload.

  4. If the field exists in the route's validate object, the value is mapped to the corresponding param type.

  5. If the route is a 'GET' method, the field is mapped as a query param.

  6. Otherwise it's mapped as a payload field.

  7. Maps "Authorization" attribute from query or data object if possible and not already mapped.

Access socket during hapi request

You can access both the socket.io server and socket within the hapi route.

exports.register = function(server, options, next) {
 
  server.route({
    method: 'GET',
    path: '/users/{id}',
    config: {
      plugins: {
        'hapi-io': 'get-user'
      }
    },
    handler: function(request, reply) {
      var io = request.plugins['hapi-io'].io;
      var socket = request.plugins['hapi-io'].socket;
 
      reply({ success: true });
 
      if (socket) {
        // socket is only defined during a hapi-io/socket.io request
      }
    }
  });
};
Post event hook

You can do further processing on a socket.io event, after it has been processed by hapi.

You can use the post option to specify a function, with two parameters: ctx and next. ctx has the following properties:

  • io - the socket.io Server object
  • socket - the socket.io Socket object
  • event - the socket.io event
  • data - the event's data object
  • req - the request object that was injected into hapi
  • res - the result object that was returned by hapi
  • result - the res.result
  • trigger - a method that allows you to trigger another socket.io event
server.route({
  method: 'POST',
  path: '/rooms/{roomId}/join',
  config: {
    plugins: {
      'hapi-io': {
        event: 'join-room',
        post: function(ctx, next) {
          ctx.socket.join(ctx.data.roomId);
          next();
        }
      }
    }
  },
  ...
});

Readme

Keywords

none

Package Sidebar

Install

npm i hapi-io

Weekly Downloads

291

Version

0.2.1

License

MIT

Last publish

Collaborators

  • sibartlett