Slide 16
Slide 16 text
Socket.IO
// note, io() will create an HTTP server for you
var io = require('socket.io')(80);
io.on('connection', function(socket) {
// Event based
socket.emit('event', { will: 'be received only by the socket' });
io.emit('event', { will: 'be received by everyone' });
io.volatile.emit('volatile event', { will: 'only be received if socket not
busy' });
// Rooms
socket.join('all the cool sockets');
socket.to('all the cool sockets').emit('hello cool sockets');
// Receiving events
socket.on('private message', function(from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
});
socket.on('reconnect', function() { console.log('socket reconnected'); });
socket.on('disconnect', function (){
io.emit('user disconnected');
});
});