sakib11
sakib11

Reputation: 536

How to know if a client has closed their browser in socket.io

I am using NODEJS+Socket.IO to control a raspberry pi pin. I have all the features working but I need to initiate a pin to be high if a client (user on the browser) closes the connection i.e closes their tab? Here is what I have tried

io.sockets.on('disconnect', function(){
  console.log("connection lost");
  });

Any Suggestions?

Upvotes: 3

Views: 1024

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44107

Use socket.on("disconnect"):

io.on("connection", socket => {
    socket.on("disconnect", () => console.log("Connection Lost"));
});

Upvotes: 8

Related Questions