skip to Main Content

i want to send data from client side to server side when user disconnect from socket.io connection with Nodejsyour text i have try to make another one event and called under disconnect event which is not possible because connection is closed.

2

Answers


  1. When a user disconnects from a Socket.IO connection, you can send data from the client-side to the server-side by emitting an event just before the disconnection event occurs. The key is to send the data before the connection is fully closed. Here’s how you can achieve this:

    // Emit an event with data just before disconnecting

    socket.emit('disconnecting', { someData: 'This is some data' });
    

    // Disconnect from the server

    socket.disconnect();
    
    Login or Signup to reply.
  2. On client side, you can use

    window.addEventListener('unload',()=>{
    //Emit websocket/socket.io event here
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search