Created
February 4, 2014 02:12
-
-
Save ryanlelek/8796313 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
'use strict'; | |
// I'd strongly recommend not using Socket.io rooms | |
// This will make it more difficult to switch to another | |
// WebSocket library that may not support rooms (SockJS?) | |
// You can use Redis or another data store to create rooms and add/remove users | |
// But, here it is for historical and educational purposes | |
// Socket.IO v0.7 now gives you one Socket per namespace you define: | |
var room1 = io.connect('/room1'); | |
room1.on('message', function () { | |
// chat socket messages | |
}); | |
room1.on('disconnect', function () { | |
// chat disconnect event | |
}); | |
var room2 = io.connect('/room2'); | |
room2.on('message', function () { | |
// chat socket messages | |
}); | |
room2.on('disconnect', function () { | |
// chat disconnect event | |
}); | |
// With different sockets, you can selectively send to the specific namespace you would like. | |
// Socket.IO v0.7 also has concept of "room" | |
io.sockets.on('connection', function (socket) { | |
socket.join('room x'); | |
socket.broadcast.to('room x').send('hello'); | |
io.sockets.in('room y').emit('ohai'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment