module.exports = function(io) {

    io.on('connection', function(socket) {

        socket.emit('welcome');

        socket.on('viewer', function(filename) {
            socket.join(filename);
        });

        socket.on('speaker', function(filename, socketId) {
            socket.join(filename);
            socket.speakerId = socketId;
        });

        socket.on('change-slide', function(filename, pageNumber) {
            socket.broadcast.to(filename).emit('update', pageNumber);
        });

        socket.on('pointer', function(filename, id, x, y) {
            if (id === socket.speakerId) {
                socket.broadcast.to(filename).emit('pointer', 'speaker', x, y);
            } else {
                socket.broadcast.to(filename).emit('pointer', 'speaker', x, y);
            }
        });

        socket.on('viewer-laser', function(filename, enabled) {
            socket.broadcast.to(filename).emit('viewer-laser', enabled);
        });

    });

}
