Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created September 26, 2011 23:41
Show Gist options
  • Save cheeyeo/1243781 to your computer and use it in GitHub Desktop.
Save cheeyeo/1243781 to your computer and use it in GitHub Desktop.
socket.io nodejs channels
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