Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display mouse pointer movement in other client computers using socket.io

I'm using socket.io engine to track the mouse pointer movement from the browser and send it to multiple web browsers. Here I get the x, y coordinates of the mouse pointer and then send it using socket.io engine.

socket.emit('mouse_position', {mx : x, my : y});

after that I get the relevant data from the application by following code

socket.on('mouse_position', function(data) {
    socket.broadcast.emit('mouse_position_update', data);
});

the I use a raphael object to show the mouse pointer in other browsers and use animate function to display the mouse pointer according to the parent mouse pointer movement.

var paper = new Raphael(canvas, 200, 200);
var cur = paper.circle(0, 0, 3);
cur.animate({
    cx : data.mx,
    cy : data.my
}, 1, 'linear');

the problem in this code is that if I lot of users (>100) log into this system , lot of bandwidth will be used and system may get crashed. can anybody tell me is there a better way to implement this system using socket.io engine or is there any algorithm we can use to solve the bandwidth issue. Any kind of help would be a great help for me .

like image 714
Gayan Charith Avatar asked Jul 06 '14 18:07

Gayan Charith


People also ask

Does Socket.IO use polling?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

How do I send to a specific socket IO client?

You can try the code below:io.to(socket.id). emit("event", data); whenever a user joined to the server, socket details will be generated including ID. This is the ID really helps to send a message to particular people.

Is Socket.IO same as WebSocket?

What Socket.IO is​ Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.


1 Answers

You should decrease the amount of time you send you mouse coordinates. In your way every pixel your mouse is changed, your data is sent. (put console.log(1) in your callback and move your mouse for a few seconds).

You can decrease the amount of time you send you data in the following way:

var prevMouseX, prevMouseY;
var intervalID = window.setInterval(function(){
   ... you get your mouse coordinates

   if (prevMouseX !== x || !prevMouseY !== y) {
      socket.emit('mouse_position', {mx : x, my : y});
   }
}, 500);

This way you will send your mouse coordinates every 0.5 seconds and only if they changed.

like image 89
Salvador Dali Avatar answered Sep 30 '22 19:09

Salvador Dali