0.2.0 • Published 7 years ago

socket.io-routes v0.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

socket.io-routes Build Status Coverage Status

NPM

An alternative API for writing socket.io event handlers

How to use

const io = require('socket.io');
const routes = require('socket.io-routes');
//...
io.use(routes);

io.on('connection', function(s) {
  s.route('event')
    .process(function(data) {
      s.emit('success', 'some data');
    });
});

Instead of sending manually a message, you can return a value to the callback of the event (if available):

s.route('event')
  .process(function(data) {
    doSomething().then(() => {
      return doSomething();
    });
  });

You can add authentication (assumes an authentication middleware like the following):

io.use(function authenticateMiddleware(s, next) {
  if (!s.auth) {
    s.auth = {};
  }

  if (s.handshake.query.token === 'secret') {
    s.auth.isAuthenticated = true;
  }

  next();
});
//...
s.route('event')
  .auth({
    isAuthenticated: true
  })
  .process(function(data) {
    return doSomething();
  });

And you can also validate data using node-validator:

s.route('event')
  .validate({
    email: {
      isEmail: {},
      isLowercase: true
  })
  .process(function(data) {
    return doSomething();
  });
0.2.0

7 years ago

0.1.0

8 years ago