Arihant
Arihant

Reputation: 4047

Broadcast mouse events to all nodes connected using node.js and socket.io

Is there a way to broadcast mouse events such as mousedown, mouseup and mousemove to all nodes connected to the nodejs server? I am trying to replicated events at certain (x,y) coordinates. Is there a way this can be achieved?

Upvotes: 1

Views: 268

Answers (1)

gevorg
gevorg

Reputation: 5055

Yes it is

Client

window.addEventListener('mousedown', function(event) {
   var data = extractMouseData(event); //extract data from event.
   io.emit('mousedown', data);
});

io.on('mousedown', function(data) {
   processMouseEvent(data);
});

Server

io.on('connection', function (socket) {
  socket.on('mousedown', function (data) {
    socket.broadcast.emit('mousedown', data);
  });
});

Just implement extractMouseData and processMouseEvent functions and make them to do what you want to do and here you go.

Upvotes: 1

Related Questions