O'Reilly logo

Mastering Node.js - Second Edition by Kevin Faaborg, Sandro Pasquali

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Drawing collaboratively

Let's create a collaborative drawing application using socket.io and Node. We want to create a blank canvas that will simultaneously display all the pen work being done by connected clients:

From the server end, there is very little to do. When a client updates coordinates by moving their mouse, the server simply broadcasts this change to all connected clients:

io.sockets.on('connection', socket => {  let id = socket.id;  socket.on('mousemove', data => {    data.id = id;    socket.broadcast.emit('moving', data);  });     socket.on('disconnect', () => {    socket.broadcast.emit('clientdisconnect', id);  });});

socket.io automatically ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required