This node.js library seeks to combine express and socket.io into one cohesive library. This project started as a fork of express.io.
Attention!
I've started this project recently - so I may make breaking changes between releases, please check the README for each release for the latest documentation.
Getting started
First install:
npm install express.oi
Then, simply replace this line of code
require('express')
with this line of code
require('express.oi')
Your app should run just the same as before! express.oi is designed to be a superset of express and socket.io.
Usage
Setting up the app
var express = require('express.oi');
var app = express();
app.http().io();
app.io.session({
secret: 'express.oi makes me happy',
resave: false,
saveUninitialized: true
});
app.listen(3000);
express.oi routes
var messages = [];
app.io.route('messages', {
list: function(req, res) {
res.json(messages);
},
add: function(req, res) {
var data = req.data;
var message = {
text: req.param('text')
};
messages.push(message);
res.status(200).json(message);
},
remove: function(req, res) {
res.sendStatus(403);
}
});
Forwarding express routes
Regular express routes can be forwarded to express.oi routes
app.route('/messages')
.get(function(req, res) {
req.io.route('messages:list');
})
.post(function(req, res) {
req.io.route('messages:add');
})
.delete(function(req, res) {
req.io.route('messages:remove');
});
More API Examples
app.io.route('examples', {
example: function(req, res) {
res.status(200)
.json({
message: 'This is my response'
});
if (req.isSocket) {
req.socket.emit('message', 'this is a test');
req.socket.broadcast.emit('message', 'this is a test');
req.socket.broadcast.to('game').emit('message', 'nice game');
req.socket.broadcast.to(socketId).emit('message', 'for your eyes only');
}
app.io.emit('message', 'this is a test');
app.io.in('game').emit('message', 'cool game');
}
});