0

I wanted to update Database when the user closes the tab . I tried using onbeforeunload and onunload which does not work every time , the user closes the tab . Thanks

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Charu
  • 9
  • _"which does not work every time"_ - presumably because the tab gets closed before these methods can complete. By the way I've changed your tags because this is a client-side question, not a server-side issue. – ProgrammingLlama Jun 04 '21 at 01:45

1 Answers1

0

You can try with socketio on server side, it allows you subscribe your client everytime you access the page with on connection method and automatically detect close tab event with on disconnect method.

const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
  client.on('event', data => { /* … */ });
  client.on('disconnect', () => { /* … */ });
});
server.listen(3000);
btb
  • 180
  • 1
  • 4