Last active
February 2, 2018 20:01
-
-
Save natebeaty/96806eecd39e18da25c163f666b0e896 to your computer and use it in GitHub Desktop.
socket.io + redis notifications server
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
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