socket.io-routes
v0.2.0
Published
An alternative API for writing socket.io event handlers
Downloads
2
Readme
socket.io-routes
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();
});