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 8790867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:45:43+00:00 2026-06-13T22:45:43+00:00

I am building a real-time web-application with multiple clients and a node.js server to

  • 0

I am building a real-time web-application with multiple clients and a node.js server to handle state and broadcast event changes from clients. I have implemented Socket.io as transport mechanism between clients and server. When a client performs a specific action an event this is send to the server, which is broadcasted to other clients on the network.

However, sometimes when an array of events is recieved by the server, the server doesn’t broadcast it but instead sends it back to the sending client – or sends it to all connected sockets including the sending client.

Here is a snippet of the server implementation, which basically listens for client events, collects them in an array which is broadcasted every 2 seconds.

If you want, I can include relevant snippets from the client implementation.

// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io'), users = require('./users');

// Commands to client
var USER_LOGIN = 0,
    USER_LIST = 1,
    USER_CONNECTED = 2,
    USER_DISCONNECTED = 3,
    COMMAND = 4,
    COMMAND_SETNICKNAME = 0,
    COMMAND_POSITION = 1,
    COMMAND_MOUSEDOWN = 2,

var array = [];
var activeUsers = {};


// Start the server at port 8080
var server = http.createServer(function(req, res){ 
    res.writeHead(200,{ 'Content-Type': 'text/html' }); 
});
server.listen(8080);

// Create a Socket.IO instance, passing it our server
var io = io.listen(server);

// Add a connect listener
io.sockets.on('connection', function(client){ 
    console.log("started");


    // Create periodical which ends a JSON message to the client every 2 seconds
    var interval = setInterval(function() {
        if(array.length >0) {
            console.log("Sending to clients " + array);
        //client.send(JSON.stringify(array));
        client.broadcast.send(JSON.stringify(array));
        array = [];
    }
},2000);

// Success!  Now listen to messages to be received
client.on('message',function(event){ 
        console.log("--Reading event " + event);
        var eventArray = JSON.parse(event);
    for(var i = 0; i < eventArray.length; i++) {
        var dataArray = eventArray[i];
        var userId = parseInt(dataArray[0]);
        var position = 1;

        console.log("current element in array " + dataArray);

        switch (parseInt(dataArray[position++])) {
            case USER_LOGIN: 
                // Log in user with USERID. 
                var username = dataArray[position++];
                console.log("Attempting to log in with user " + username);

                // If valid, send USER_LOGIN broadcast to clients
                if(login(username)) {
                    array.push([0, USER_LOGIN, login(username)]);
                }
                break;
  • 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-13T22:45:44+00:00Added an answer on June 13, 2026 at 10:45 pm
    io.sockets.on('connection', function(client){ 
        console.log("started");
    
    
        // Create periodical which ends a JSON message to the client every 2 seconds
        var interval = setInterval(function() {
            if(array.length >0) {
                console.log("Sending to clients " + array);
            //client.send(JSON.stringify(array));
            client.broadcast.send(JSON.stringify(array));
            array = [];
        }
    },2000);
    

    you are creating one scheduler for each connection which is going to be a mess. Because you are creating the interval inside on('connection',
    And I don’t see any point of calling clients in an interval because you have a method to push the changes on some clients change event. That is the advantage of socket.io . Instead of using an interval you can just broadcast the change in another listener from client change event like this.

    client.on('onSomechangeInClient',function(event){ 
      //update array here..
     client.broadcast.send(JSON.stringify(array));
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a web application that has a real-time feed (similar to Facebook's
I have been building a real-time notification system. It’s part of a web application,
I'm building a real-time web application As far as I know, the most popular
We are building a financial web application which requires a real time chart. Initially
I am building a client server real time communication application in php, where i
I have already asked a question regarding a simple fault-tolerant soft real-time web application
Im building a web app with some real-time features, chat, auto updating lists. What's
I am building a real-time multi-player browser-based game using node.js. At the moment I
I'm building a real-time GPS tracking system, which will receive GPS data sent from
I'm building a real time game, mostly chat based, and I need to have

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.