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 ...

Get Mastering Node.js - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.