Socketio basics!
On Client Side
// create a socket
// if url is not provided, then it connects to the default existing server
var socket = io(url);
// listen to custom events
socket.on('privateMessage', function(data) {
/** when `privateMessage` event is fired **/
});
socket.on('universalMessage', function(data) {
/** when `universalMessage` event is fired **/
});
// existing events for socket object
socket.on('connect', function() { /** when socket connects **/ });
socket.on('error', function() { /** error occured **/ });
socket.on('disconnect', function() { /** when socket disconnects **/ });
socket.on('reconnect', function() { /** when socket reconnects **/ });
On Server Side
io.on('connection', function(socket) { /** a new socket connects **/ });
List all the sockets
io.sockets.sockets /** array of sockets **/
Namespace
The path of a socket default namespace :
/
// emits
io.emit('some event', 'this is a message');
// captures
io.on('connection', function(socket){
/** a socket connected **/
});
Custom namespace
// create a custom namespace on server side
var customNameSpace = io.of('/my-custom-namespace');
// emits
customNameSpace.emit('some event', 'this is a message');
// captures
customNameSpace.on('connection', function(socket){
/** a socket connected **/
});
Connect to a namespace on client side
var socket = io('/my-custom-namespace');
Room
Within each namespace, you can also define channels that sockets can join and leave.
// join a `room`
io.on('connection', function(socket){
socket.join('some room');
});
// leave a `room`
io.on('connection', function(socket){
socket.leave('some room');
});
// broadcast to entire `room`
io.to('some room').emit('some event');
// or
io.in('some room').emit('some event');
The Default room
Each Socket in Socket.IO is identified by a unique identifier Socket#id and automatically joins a room identified by this id.
Debugging
On client side (browsers)
localStorage.debug = '*';
On server side (node)
$ DEBUG=* node yourfile.js
Cheatsheet
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.sockets.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
io.sockets.in('game').emit('message', 'cool game');
// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');
// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');
// sending to individual socketid
io.sockets.socket(socketid).emit('message', 'for your eyes only');