Skip to content

Provide an easy way to get all net.Server connections #48433

Description

@qiulang

What is the problem this feature will solve?

Currently, there seems no way to get all net.Server connections with the public API.

net.server.getConnections((err, count) => {...}) only returns the count of the connection, I can't get each connection within its callback. I tried for (let socket of server.connections) {...} inside the callback, and got the error TypeError: server.connections is not iterable.

I have to store each socket myself on Event: 'connection' callback, keep an array of those socket objects.

What is the feature you are proposing to solve the problem?

Inside net.server.getConnections((err, count) => {...}) callback, in addition to returning connection count, also return all the connections.

What alternatives have you considered?

No response

Activity

  1. tniessen commented on Jun 13, 2023

    @tniessen
    Member

    A similar feature was originally proposed in #42812, but I consider it an anti-pattern in Node.js (see #42812 (review)):

    Constructing potentially large lists of resources generally feels like an anti-pattern in Node.js, and unless the application code is entirely synchronous, it is prone to race conditions.

  2. added
    netIssues and PRs related to the net subsystem.
    on Jun 13, 2023
  3. bnoordhuis commented on Jun 14, 2023

    @bnoordhuis
    Member

    Another reason not to do this is that it would be incongruent with server.getConnections()1. That method reports connections handled by worker processes but the feature being requested here can't do that because such connections are gone.

    1 Which, for the record, I think is of questionable design too.

  4. Antonius-S commented on Jun 15, 2023

    @Antonius-S

    Honestly I can't imagine what current getConnections() could be useful for at all. Anyway everyone has to implement the list of connected sockets manually to be able to close the server fully. If an app serves millions of clients, the list would only occupy several MBs in memory which is nothing compared to all the allocated socket objects. Probably it's the lookups in containers with millions of items that are problem?

  5. qiulang commented on Jun 17, 2023

    @qiulang
    Author

    @tniessen @bnoordhuis If it weren't for net.getConnections() I won't ask this feature request.
    As @Antonius-S said

    I can't imagine what current getConnections() could be useful for at all.

    Especially @bnoordhuis said "such connections are gone", although I don't get why. If some connections are gone, what is the point of using count in its callback?

  6. tniessen commented on Jun 18, 2023

    @tniessen
    Member

    I assume getConnections() to count connections is mostly used as a metric in practice, e.g., with Prometheus or so.

    And I believe what @bnoordhuis meant is that the primary process in a cluster does not have access to all connection objects because some exist in the address space / JS heap of other processes.

  7. github-actions commented on Dec 16, 2023

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

  8. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Dec 16, 2023
  9. github-actions commented on Jan 15, 2024

    @github-actions
    Contributor

    There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.netIssues and PRs related to the net subsystem.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions