Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:48:59+00:00 2026-06-12T22:48:59+00:00

So I am writing a small ‘co-browsing’ demo. Mostly for educational purposes. Anyway, the

  • 0

So I am writing a small ‘co-browsing’ demo. Mostly for educational purposes.

Anyway, the plan is this. Users open their browser to my server and move their mouse around. When doing so, the browser sends the mouse coordinates to the server and the server relays the coordinates back to any browser subscribing to the channel. The coordinates are then displayed in a text box.

I am using nodejs and socket.io.

Here is my server.js:

var app = require('http').createServer(handler),
    io = require('socket.io').listen(app),
    fs = require('fs');


app.listen(80);

function handler(req, res) {
    var file = __dirname + '/public/index.html';
    fs.readFile(file, 
        function(err, data) {
            if(err) {
                res.writeHead(500);
                return res.end('Error loading index.html');
            }

            res.writeHead(200);
            res.end(data);
        }
    );
}


io.sockets.on('connection', function(socket) {
    socket.on('m', function(data) {
        socket.emit('relay', {msg: 'MouseX: ' + data.x + ' MouseY: ' + data.y});
    });
});

Here is my client:

<!doctype html>
<html>
    <head>
    </head>
    <body>

        <input id='box' type='text' size='200' />

        <script src='/socket.io/lib/socket.io.js'></script>
        <script>

            var socket = io.connect('http://localhost');
            var b = document.getElementById('box');

            socket.on('relay', function(data) {
                b.setAttribute('value', data.msg);
            });

            document.onmousemove = function(event) {
                event = event || window.event;
                socket.emit('m', {x: event.clientX, y: event.clientY});
            }


        </script>

    </body>
</html>

Now, if I open two browsers and point both to localhost I can move the mouse over a window and the textbox at the top works. It shows the coordinates. However, the OTHER browser doesn’t do anything. But if I move the mouse over to the other browser, it then displays the coordinates on its window. It’s like it only relays to the browser I am currently hovering over. The server console shows movements on both browsers.

But since both browser windows are responding to the relay event (channel), shouldn’t both browsers echo the coordinates? So that browser 1’s mouse movement is echoed to browser 2 and when I go over to browser two, its coordinates are echoed over to browser 1?

Hope that makes sense.

Thanks

** SOLUTION **

Thanks to Victor Stanciu for the quick answer. Here is what I did to fix the server.

var app = require('http').createServer(handler),
    io = require('socket.io').listen(app),
    fs = require('fs');


app.listen(80);

function handler(req, res) {
    var file = __dirname + '/public/index.html';
    fs.readFile(file, 
        function(err, data) {
            if(err) {
                res.writeHead(500);
                return res.end('Error loading index.html');
            }

            res.writeHead(200);
            res.end(data);
        }
    );
}

var sockets = [];
io.sockets.on('connection', function(socket) {
    sockets.push(socket);

    socket.on('m', function(data) {
        io.sockets.emit('relay', {msg: 'MouseX: ' + data.x + ' MouseY: ' + data.y});
    });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:49:00+00:00Added an answer on June 12, 2026 at 10:49 pm

    That’s because you are only emmiting to the socket that you receive the signal from:

    io.sockets.on('connection', function(socket) {
        socket.on('m', function(data) {
            socket.emit('relay', {msg: 'MouseX: ' + data.x + ' MouseY: ' + data.y});
        });
    });
    

    This says: “When the ‘m’ signal is received from ‘socket’, emit the ‘relay’ signal to it”.

    What you have to do is this
    – when a new socket is connected, add it to an array
    – when a socket emits a signal, iterate over all the connected sockets and emit the ‘relay’ signal to each of them

    var sockets =  [];
    io.sockets.on('connection', function(socket) {
        sockets.push(socket);
    
        socket.on('m', function(data) {
            sockets.forEach(function (socket) {
                socket.emit('relay', {msg: 'MouseX: ' + data.x + ' MouseY: ' + data.y});
            });
        });
    });
    

    Alternatively, rather that manually iterating through the connected sockets, socket.io provides a way to do this:

     io.sockets.emit('this', { will: 'be received by everyone'});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small jQuery plugin to allow inline editing (for my purposes
I am new to network programming, and have been learning this by writing small
I'm currently writing a small script using pygame, but I don't believe this question
Writing a small app that ( among other things ) lets users upload a
I'm writing small XMPP server using boost::asio and I want to unit-test my code.
I am writing small app, using Play Framework 2.0 which uses Ebean as ORM.
I have an application where I am reading and writing small blocks of data
I'm trying to practice my F# by writing small console scripts in F# in
Coming from a PHP background, I'm used to writing small functions that return a
I am writing a small program that sends and receive multicast packets.I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.