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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:11:58+00:00 2026-05-22T21:11:58+00:00

I am using node.js building a TCP server, just like the example in the

  • 0

I am using node.js building a TCP server, just like the example in the doc. The server establishes persistent connections and handle client requests. But I also need to send data to any specified connection, which means this action is not client driven. How to do that?

  • 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-05-22T21:11:59+00:00Added an answer on May 22, 2026 at 9:11 pm

    Your server could maintain a data structure of active connections by adding on the server “connection” event and removing on the stream “close” event. Then you can pick the desired connection from that data structure and write data to it whenever you want.

    Here is a simple example of a time server that sends the current time to all connected clients every second:

    var net = require('net')
      , clients = {}; // Contains all active clients at any time.
    
    net.createServer().on('connection', function(sock) {
      clients[sock.fd] = sock; // Add the client, keyed by fd.
      sock.on('close', function() {
        delete clients[sock.fd]; // Remove the client.
      });
    }).listen(5555, 'localhost');
    
    setInterval(function() { // Write the time to all clients every second.
      var i, sock;
      for (i in clients) {
        sock = clients[i];
        if (sock.writable) { // In case it closed while we are iterating.
          sock.write(new Date().toString() + "\n");
        }
      }
    }, 1000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using node.js, the net module for building a tcp server which can hande http
I'm building a simple system like a realtime news feed, using node.js + socket.io.
I am using node.js to build a tcp server, and I want to extract
I am using node.js to build a TCP server and I got the following
I'm interested in using Node.js as a socket server for stream-based communication with tens
How can I just obtain the string value of a Xml node using xpath
I am building a Windows form application using .NET 3.5. I would like to
I'm just building a table to store hierarchical data using the Modified Pre-order Tree
I am building a file synchronization program (not unlike Dropbox) using node.js on both
I'm building a server in Node.js that receives data from Javascript on a website.

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.