Created
September 26, 2011 23:41
-
-
Save cheeyeo/1243781 to your computer and use it in GitHub Desktop.
socket.io nodejs channels
This file contains hidden or 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
io.sockets.on('connection', function (socket) { | |
socket.on('message', function(topic){ | |
// adds this socket to the particular topic | |
socket.join(trend); | |
}); | |
// sends a message to all connected clients of topic 'my-trend' | |
socket.broadcast.to('my-topic').emit('a new post'); | |
socket.on('disconnect', function(topic){ | |
// remove the client from the specified topic | |
// future broadcasts will not be sent to the topic | |
socket.leave(topic); | |
}); | |
}); | |
// accessing the socket based on the channel/room name | |
var socket = io.sockets.in('topic'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment