socketio / socketio/socket.io

Socket disconnect event doesn't always fire

Open
#5,246 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

to triage
Dominant language
TypeScript
Stars
63.2k
Forks
10.3k
Avg merge
11d 20h
Merged PRs (30d)
2

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository files or tests are named, and the report has no reproduction steps. Start by reproducing the provided Socket.IO client/server setup on iOS 18, then trace the disconnect and connection-health behavior involved in the stale sockets. Done means identifying the cause and adding a regression test or clearly documenting the confirmed limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react-native
Domain
mobile-dev, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.