-
-
Save fajarlabs/aa73cc21977759763c94262d892be259 to your computer and use it in GitHub Desktop.
A simple example of setting-up dynamic "rooms" for socket.io clients to join
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
// set-up a connection between the client and the server | |
var socket = io.connect(); | |
// let's assume that the client page, once rendered, knows what room it wants to join | |
var room = "abc123"; | |
socket.on('connect', function() { | |
// Connected, let's sign-up for to receive messages for this room | |
socket.emit('room', room); | |
}); | |
socket.on('message', function(data) { | |
console.log('Incoming message:', data); | |
}); |
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
// attach Socket.io to our HTTP server | |
io = socketio.listen(server); | |
// handle incoming connections from clients | |
io.sockets.on('connection', function(socket) { | |
// once a client has connected, we expect to get a ping from them saying what room they want to join | |
socket.on('room', function(room) { | |
socket.join(room); | |
}); | |
}); | |
// now, it's easy to send a message to just the clients in a given room | |
room = "abc123"; | |
io.sockets.in(room).emit('message', 'what is going on, party people?'); | |
// this message will NOT go to the client defined above | |
io.sockets.in('foobar').emit('message', 'anyone in this room yet?'); |
// sending to spesific user with same topic
io.to(socket_id).emit('user_online', {say:"hello"});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
socket.emit('message', "this is a test"); //sending to sender-client only
socket.broadcast.emit('message', "this is a test"); //sending to all clients except sender
socket.broadcast.to('game').emit('message', 'nice game'); //sending to all clients in 'game' room(channel) except sender
socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel)
socket.broadcast.to(socketid).emit('message', 'for your eyes only'); //sending to individual socketid
io.emit('message', "this is a test"); //sending to all clients, include sender
io.in('game').emit('message', 'cool game'); //sending to all clients in 'game' room(channel), include sender
io.of('myNamespace').emit('message', 'gg'); //sending to all clients in namespace 'myNamespace', include sender
socket.emit(); //send to all connected clients
socket.broadcast.emit(); //send to all connected clients except the one that sent the message
socket.on(); //event listener, can be called on client to execute on server
io.sockets.socket(); //for emiting to specific clients
io.sockets.emit(); //send to all connected clients (same as socket.emit)
io.sockets.on() ; //initial connection from a client.