I'm new to node and socket io. I'm trying to implement a realtime notification system for couple of my own apps. So, using node, express and socket io, the code is given below:
Server Side Code:
io.on('connection', function (socket) {
socket.on('subscribe', function(room) {
socket.join(room);
});
socket.on('unsubscribe', function(room) {
socket.leave(room);
});
});
Client Side Code:
var sio = io.connect('http://localhost:9000');
var ch1 = sio.emit('subscribe', 'channel1');
ch1.on('log', function (data) {
console.log('channel1: ', data);
});
var ch2 = sio.emit('subscribe', 'channel2');
ch2.on('log', function (data) {
console.log('channel2: ', data);
});
I'm firing/emitting the event from a route (express) for example:
app.get('/', function(req, res) {
var data1 = {
channel: 'channel1',
evennt: 'log',
message: 'Hello from channel1...'
};
io.to(data1.channel).emit(data1.event, data1);
});
When I'm hitting the route, the io.to(data1.channel).emit(data1.event, data1); is working but it sending the data to both rooms/channels but I was expecting to get the data only in ch1 because data1.channel contains channel1 so I was expecting the following handler will receive the data:
ch1.on('log', function (data) {
console.log('channel1: ', data);
});
Notice that, both channels have same log event. Am I on the right track. Is it possible at all?