Created
February 26, 2016 11:42
-
-
Save NizarOukhchi/eb077d3e50667c691765 to your computer and use it in GitHub Desktop.
Socket.io config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* app/server/config/sockets.js */ | |
var config = require('./environment'); | |
var redis = require('redis'); | |
var initialized = false; | |
var ioEmitter; | |
var redisClient = redis.createClient(config.redis.url, {return_buffers: true}); | |
var pub = redis.createClient(config.redis.url); | |
var sub = redis.createClient(config.redis.url, {return_buffers: true}); | |
module.exports = { | |
init: function (server) { | |
if (initialized) return ioEmitter; | |
var io = require('socket.io')(server, { | |
serveClient: true | |
}); | |
io.adapter(require('socket.io-redis')({key: '', pubClient: pub, subClient: sub})); | |
ioEmitter = require('socket.io-emitter')(redisClient); | |
io.on('connection', function (socket) { | |
/* quand le client se connect ça se passe ici */ | |
}); | |
initialized = true; | |
return ioEmitter; | |
}, | |
broadcast: function (event, payload) { | |
ioEmitter.of('/').emit(event, payload); | |
}, | |
emit: function (to, event, payload) { | |
ioEmitter.of('/').to(to).emit(event, payload); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment