Skip to content

Socket disconnect event doesn't always fire #5246

Open
@nzapponi

Description

@nzapponi

Describe the bug
We have a React Native app using Socket.io to communicate with our servers.
Some users experience a quirk whereby leaving the app doesn't fire a disconnect event on the server for a very long time (hours).

See screenshot of some of our logs:
Screenshot 2024-11-29 at 23 20 42

This also occurs upon making a io.in([...]).fetchSockets() call: those sockets still look open.

To Reproduce
N/A

Socket.IO server version: 4.8.1

Server

import { Server } from "socket.io";

const io = new SocketServer<
      ClientToServerEvents,
      ServerToClientEvents,
      { [event: string]: (...args: unknown[]) => void },
      SocketData
    >(server, {
      path: "/socket",
      serveClient: false,
      cookie: true,
      adapter: socketIoPubSubTopic
        ?
          createAdapter(socketIoPubSubTopic)
        : undefined,
    });

    if (socketIoPubSubTopic) {
      await io.of("/").adapter.init();
      log.log("Pub/sub subscription initialized for Socket.IO");
    }

    io.on("connection", async (socket) => {
    const userId = socket.data.userId;
    log.log({ userId }, `Socket connected for ${userId}`);

    if (userId) {
      await updateJoinedRooms(userId, socket);
    }

    socket.on("disconnect", () => {
      log.log({ userId }, `Socket disconnected for ${userId}`);
    });
  });

Socket.IO client version: 4.8.1

Client

import { io } from "socket.io-client";

const newSocket: TypedSocket = io(PUBLIC_SOCKET_HOST, {
        auth: !useCookies
          ? {
              token,
            }
          : undefined,
        path:"/socket",
        ackTimeout: 10000,
        retries: 12,
        timeout: 5000,
        reconnectionDelayMax: 5000,
        withCredentials: useCookies,
      });

Expected behavior
To always get the disconnect event firing.

Platform:

  • Device: iPhone 14 Plus
  • OS: iOS 18

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    to triageWaiting to be triaged by a member of the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions