Socket io dosen't detect always disconnect
See original GitHub issueDisconnecting 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closed due to inactivity, please reopen if needed.
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?