Reputation: 575
Currently sails.io is using socket.io 1.7.1. It is not going to change in sails 1.0. So I was planning to use socket.io 2.0 using the socket.io package instead of relying on sails real-time layer. According to socket.io documentation, it is quite easy to implement in express as follows:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
How can we write the same socket.io 2.0 implementation in sails?
Upvotes: 1
Views: 142
Reputation: 24958
The underlying Express server used in a Sails 1.0 app is exposed as sails.hooks.http.server
. So you can piggyback on top of that in your app's config/bootstrap.js
file:
module.exports.bootstrap = function (done) {
var io = require('socket.io')(sails.hooks.http.server);
io.on('connection', function(socket){
console.log('a user connected');
});
}
You'll also need to turn off the default sockets
and pubsub
hooks, in your .sailsrc
file:
"hooks": {
"sockets": false,
"pubsub": false
}
This also has the benefit of freeing up the sails.io
global, so you can re-use that for your own implementation.
Upvotes: 2