Created
July 25, 2015 14:04
-
-
Save erickgnavar/91b53732704cd6b35fd6 to your computer and use it in GitHub Desktop.
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 io = require('socket.io').listen(9000, {log: false}); | |
var redis = require("redis").createClient(); | |
redis.subscribe('chat'); | |
redis.subscribe('notifications'); | |
var chat = io.of('/chat'). | |
on('connection', function (socket) { | |
socket.on('register room', function (room) { | |
socket.set('room', room); | |
}); | |
redis.on('message', function (channel, message) { | |
var messageObject = JSON.parse(message); | |
if (channel === 'chat') { | |
socket.get('room', function (err, room) { | |
if (err === null && room !== null) { | |
if (messageObject.room.id === room.id) { | |
socket.emit('message', message); | |
} | |
} | |
}); | |
} | |
}); | |
}); | |
var notifications = io.of('/notifications'). | |
on('connection', function (socket) { | |
console.log(socket.id); | |
socket.on('register user', function (user) { | |
socket.set('user', user); | |
}); | |
redis.on('message', function (channel, message) { | |
var messageObject = JSON.parse(message); | |
if (channel === 'notifications') { | |
socket.get('user', function (err, user) { | |
if (err === null && user !== null) { | |
if (user.id.toString() === messageObject.recipient.id.toString()) { | |
socket.emit('notification', message); | |
} | |
} | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment