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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:36:16+00:00 2026-06-12T12:36:16+00:00

With node.js, I’m trying to send the current server_time to all clients in every

  • 0

With node.js, I’m trying to send the current server_time to all clients in every second.
Therefore, I wanted to use setInterval() to emit an event to all clients and sending the time, but it doesn’t work. Did I define the setInterval function at the right place or did missed something else?

var http = require("http");
var socketIO = require('socket.io');
var connect = require('connect');

//keep track of every connected client
var clients = {};

//create Server 
var httpServer = connect.createServer(
    connect.static(__dirname)
).listen(8888);


//socket
var io = socketIO.listen(httpServer);
io.sockets.on('connection', function (socket) {

    //add current client id to array
    clients[socket.id] = socket;
    socket.on('close', function() {
        delete clients[socket.fd]; // remove the client.
    });

    //send news on connection to client
    socket.emit('news', { hello: 'world' }); 

    //this one works fine!
    //send server time on connection to client
    socket.emit("server_time", { time: new Date().toString() });

});

//this doesn't work!
// Write the time to all clients every second.
setInterval(function() { 
    var i, sock;
    for (i in clients) {
        sock = clients[i];
        if (sock.writable) { // in case it closed while we are iterating.
            sock.emit("server_time", {
                console.log("server_time sended");
                time: new Date().toString()

            });
        }
    }
}, 1000);       //every second
  • 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-12T12:36:17+00:00Added an answer on June 12, 2026 at 12:36 pm

    May I suggest a workaround/improvement that should fix the problem. Add the clients to a chat room. Somewhere in:

    io.sockets.on('connection', function (socket) {
    

    add a

    socket.join('timer');
    

    Then the setIntervall would be

    setInterval(function() { 
        io.sockets.in('timer').emit("server_time", { time: new Date().toString() })
    }, 1000); 
    

    Hope this works for you!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With Node I am trying to collect user data from an LDAP server and
In Node.js is there any way to listen to all events emitted by an
In Node.js, I'm trying to get the line number of each function that is
node version is 0.6.17. I was trying to get the number of bytes of
Node.getTextContent() returns the text content of the current node and its descendants. is there
Since Node is asynchronous, I'm having problems trying to get a callback to return
Using Node.js and socket.io, the following works fine unti trying to broadcast back to
On Node.js using Express, I am outputting JSON with a simple response.send(myObject) It's working
Node of the list where every element points to next element and the head
any node above the current node in an hierarchical tree example the parent,grandparent,great-grandparent or

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.