socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Socket disconnect event doesn't always fire

Open nzapponi opened this issue 1 year ago • 0 comments

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.

nzapponi avatar Nov 29 '24 23:11 nzapponi