I'm new to node.js and socket.io. I've got a test server with the sample code from the socket.io site (http://socket.io/#how-to-use) running.
I don't know where to begin to do the following
I guess I need to create some sort of routing in the "handler" function, serverside of the node.js app, but I'm unsure if that's the correct way to go about it.
Can somebody help pointing me in the right direction please?
Thanks a lot for helping a newb out!
Socket.io makes this really easy. It has namespaces and rooms. For example to create two different namespaces you would do this:
var io = require('socket.io').listen(app, {origins: '*:*', log: false});
var page1 = io.of('/page1').on('connection', function (socket) {
//you can use socket in here
});
var page2 = io.of('/page2').on('connection', function (socket) {
//you can use socket in here
});
You now have two namespaces. Inside of each you could listen for events. For example if you sent a type event:
socket.on('type', function(text){
io.sockets.emit('type', {'whatWasTyped': text]);
}
This will send a type event to all connected clients.
If you wanted to separate the namespaces even more you could create rooms. Here is how to create a room in a namespace:
var page1 = io.of('/page1').on('connection', function (socket) {
socket.on('add', function(area){
socket.join(area);
};
});
You would have to send an add event everytime you made a connection with the room you would want to join. You can then send messages to only that room then.
io.of('/users').in(area).emit('event', {'event': yourInfoHere});
You can get info about the connection by running:
socket.get('user', function(err, info){ //in here });
This was some info off the top of my head and from the socket.io wiki: https://github.com/LearnBoost/socket.io/wiki/Rooms.
I haven't tested it, let me know if you have a few more questions.