question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Socket io dosen't detect always disconnect

See original GitHub issue

Disconnecting dosen’t work always When Socket io disconnecting except Mongodb database to remove online user but it dosen’t work sometimes. Record in mongodb database stay sometimes especiellay if user closed the browser for long time. Socket.IO server version: 4.1.2

Server side:

import { Server, Socket } from "socket.io";
import "./config";

import { MsgModel } from "./models";
import { JoinListModel } from "./models";
import { callUserTypes } from "./typings";

const io = new Server(5000, {
  cors: {
    origin: ["http://localhost:3000"],
    credentials: true,
  },
});

io.on("connect_error", (err: any) => {
  console.log(`connect_error due to ${err.message}`);
});

io.on("connection", async (socket: Socket) => {
  console.log(`✅ Client ${socket.id} has connected!`);

  socket.emit("me", socket.id);


  socket.on("Join", async (data: any) => {
    const findIt =
      (await JoinListModel.find({ nickName: data.nickName })).length > 0;
    if (findIt) {
      socket.emit("loginMsg", "Danger");
    } else {
      const JoinList = new JoinListModel();
      JoinList.id = socket.id;
      JoinList.nickName = data.nickName;
      JoinList.joinedDatenTime = new Date();
      await JoinList.save();
      const onlineListData = await JoinListModel.find({});
      io.sockets.emit("OnlineList", onlineListData);
      socket.emit("loginMsg", "Success");
    }

    console.log(`${data.nickName} is joined!`);
  });

  socket.on("disconnect", async () => {
    await JoinListModel.deleteOne({ id: socket.id });
    console.log(`🚫 Client ${socket.id} has disconnected`);
    const onlineListData = await JoinListModel.find({});
    io.sockets.emit("OnlineList", onlineListData);
  });
});

Client:

const [socket, setSocket] = useState<Socket | null>(null);

  useEffect(() => {
   const socket = io("http://localhost:5000");
   setSocket(socket);
},[]);

Please help? Why socket io dosen’t detect that?

EDIT: edited by me (@darrachequesne) for clarity

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
darrachequesnecommented, Apr 6, 2022

Closed due to inactivity, please reopen if needed.

0reactions
fadihanna123commented, Jul 15, 2021

I tried to reload page or close the tab but sometimes it ignore removing online user from database it means that socket io doesn’t detect disconnecting always of some reason. Can you help me, please?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting connection issues | Socket.IO
First and foremost, please note that disconnections are common and expected, even on a stable Internet connection:
Read more >
Socket.io Detect when client disconnects - Stack Overflow
The problem I am currently facing is that while I can detect when a socket disconnects from my server, I don't know which...
Read more >
how do I find out why the client disconnected? - Google Groups
I have about 10-20 concurrent connections to socket.io from chrome and android. I've set closeTimeout and timeout for all 6 protocols via
Read more >
Socket.io disconnect randomly after some time. - Sololearn
Yep, I've had same issue actually. But I managed to solve it differently. There was no issue on code side, it was sever...
Read more >
Fullstack2021 | p17 | SocketIO connect and disconnect
In this series we will build a Web application with realtime communication using NestJS (Backend):https://nestjs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found