Skip to content

Instantly share code, notes, and snippets.

@natebeaty
Last active February 2, 2018 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natebeaty/96806eecd39e18da25c163f666b0e896 to your computer and use it in GitHub Desktop.
Save natebeaty/96806eecd39e18da25c163f666b0e896 to your computer and use it in GitHub Desktop.
socket.io + redis notifications server
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis').createClient({host: 'localhost'});
var port = process.env.PORT || 8890;
server.listen(port);
io.on('connection', function (socket) {
socket.on('subscribe', function(data) {
console.log('Joining room', data.room);
socket.join(data.room);
redis.subscribe(data.room);
})
socket.on('disconnect', function() {
delete io.sockets[socket.id];
});
});
redis.on('message', function(room, message) {
console.log('Redis message: ' + message + '. In room: ' + room);
io.in(room).emit('message', message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment