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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:13:52+00:00 2026-05-31T23:13:52+00:00

When someone connects to the node server, I keep an array with all the

  • 0

When someone connects to the node server, I keep an array with all the sockets.
That way I can broadcast messages to everyone whenever that is needed or loop through the users to count the number of online users, etc.

All this works fine, but when a on disconnect event is fired, I don’t receive a socket in my arguments.
Is there another way to know which socket just disconnected?

var allClients = [];

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

   socket.on('disconnect', function(socket) {
      console.log('Got disconnect!');

      var i = allClients.indexOf(socket);
      delete allClients[i];
   });
});

Of course the above example doesn’t work, because disconnect event doesn’t give a socket argument (or any other argument).
So is there another event that fired before a disconnect where the socket is still there?

Ali

  • 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-31T23:13:53+00:00Added an answer on May 31, 2026 at 11:13 pm

    You already have the socket, because the disconnect handler is declared within the ‘connection’ event scope. Try removing the parameter you are passing to the ‘disconnect’ handler, you should be able to work with the socket parameter from the connection handler.

    io.sockets.on('connection', function(socket) {
       allClients.push(socket);
    
       socket.on('disconnect', function() {
          console.log('Got disconnect!');
    
          var i = allClients.indexOf(socket);
          delete allClients[i];
       });
    });
    

    Apart from that, you don’t need a socket array to broadcast, you can use rooms to group sockets and broadcast to all the sockets inside that room.

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

Sidebar

Related Questions

Hi Hope someone can help me. My .net app that connects (via tcp) to
I'm having a little trouble with sockets in Python. Whenever someone connects it works
I'm writing a Java class that connects to a server and reads messages in
I am trying to understand the purpose of addListener in node.js . Can someone
I need to build a client application that connects to the team foundation server
Can someone please point me in the right direction to create a Mac/PC server
When someone connects to my TCP Server, I log the time they connected using
I just need a simple example how to do a Node.JS server that I'll
Could someone please explain the best way to connect to an Interbase 7.1 database
Someone told me about swamp diagrams explaning that they were useful to predict code

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.