Skip to content

Each Namespace Holds a Connection #3

Closed

Description

@dave-apex

First, we love this package and the opportunities that it opens up for us. Great job :).

We have an app that started having database connection problems after adding socket.io. After doing some digging it appears that the issue is that the postgres adapter holds a connection for each namespace. That would be fine for many apps but our design is based on custom namespaces. It appears that every namespace that is accessed by some user will then hold a connection indefinitely. Is that the expected behavior? Are there any patterns that could free up these connections when the last user disconnects from the namespace?

Activity

  1. darrachequesne commented on Dec 8, 2021

    @darrachequesne
    Member

    It appears that every namespace that is accessed by some user will then hold a connection indefinitely.

    Yes, that's indeed the current behavior, but I think we could change it.

    As a temporary workaround, you can close the connection when the last user disconnects:

    const namespace = io.of("/the-namespace");
    
    namespace.on("connection", (socket) => {
      socket.on("disconnect", () => {
        const isLast = namespace.sockets.size === 0;
        if (isLast) {
          namespace.adapter.close();
        }
      });
    });
  2. added
    enhancement
    on Dec 8, 2021
  3. dave-apex commented on Dec 8, 2021

    @dave-apex
    Author

    Thanks makes a lot of sense. In the meantime, we have moved from Namespaces to Rooms and that is working fine for us.

  4. darrachequesne commented on Apr 28, 2022

    @darrachequesne
    Member

    This should be fixed in version 0.3.0, implemented in 651e281.

  5. added this to the 0.3.0 milestone on Apr 28, 2022
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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions