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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:49:07+00:00 2026-06-01T23:49:07+00:00

I have a node.js server which keeps track of all clients connected to it,

  • 0

I have a node.js server which keeps track of all “clients” connected to it, using an associative array of sockets, because I need to send some data to specific clients sometimes. The keys in that array are strings composed of

socket.remoteAddress + ':' + socket.remotePort

and values are socket instances passed to the callback provided to net.createServer(…)
Sometimes the clients disconnect for one reason or another and I receive the ‘end’ and then ‘close’ events. I’d like to remove the disconnected clients from my client registry but in the close/end event callbacks, the remoteAddress and remotePort variables are undefined. Can they be retrieved somehow?

To illustrate:

var registry = {}
var server = net.createServer(function (socket) {
    socket.on('connect', function(){
        registry [socket.remoteAddress + ':' + socket.remotePort] = socket;
    });

    socket.on('close', function(had_error){
            // ******************************************************
            // socket.remoteAddress and remotePort are undefined here
            // ******************************************************
            var id = socket.remoteAddress + ':' + socket.remotePort;
            // *************************************************************
        if(registry.hasOwnProperty(id)
                delete socket.id;
    });
});
  • 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-01T23:49:08+00:00Added an answer on June 1, 2026 at 11:49 pm

    I think you should use socket.id instead of creating your own object property that might not be unique

    socket.on('connect', function(){
            registry [socket.id] = socket;
    });
    

    UPDATE

    Node provides basic http API. Http is stateless. HTTP sessions allow associating information with individual visitors but node doesn’t have a native sessions support.

    Take a look at express, it is a nice web framework with sessions support

    Also, if you need to send messages in a realtime fashion, take a look at socket.io

    UPDATE V2

    You can add your own ID property to the socket object:

    function randomID(len){
      //Generate a random String
      return randID;
    }
    var registry = {}
    var server = net.createServer(function (socket) {
        socket.on('connect', function(){
            var uid = randomID(32);
            socket.id = uid;
            registry [uid] = socket;
        });
    
        socket.on('close', function(had_error){
                var uid = socket.id;
                // *************************************************************
                console.log("Socket ID " + uid + " disconnected")
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my problem: I have server A, running node.js and using socket.io for communicating
We have a Scala server that is getting a node tree using Protocol Buffers
I have a socket.io server using redis called server.js that fires up a node
I have a Node server which I would like to debug. Once I've started
using node.js, the net module for building a tcp server which can hande http
I have the following node.js server-side code: var app = require('http').createServer(handler) , io =
On my node.js server I have the following code: //Generate a token var token
I'm trying to implement an http long polling server in Node.js, and have no
How to send messages from php to node.js? I have a linux server running
In the sitemap file, I have sitemap Node URL which have more than 1

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.