Ras
Ras

Reputation: 1031

Socket.io don't emit after disconnect event

I want to send data from the server to the client after a disconnect socket.io event occurs.

Server :

const clients = {};
io.on('connection', socket => {
    clients[socket.id] = socket;
    console.log('connect : ' + socket.id);
    socket.emit('socket-connect', 'connected');

    socket.on('disconnect', () => {
        socket.emit('socket-disconnect', 'disconnected');
        delete clients[socket.id];
        console.log('disconnect : ' + socket.id);
    });
});

Client :

const  socket = openSocket('localhost');

socket.on('socket-connect', data => {
    console.log(data);
});

socket.on('socket-disconnect', data => {
    console.log(data);
});

If socket connected, the server send a connect message to client and it works, but it does not send interrupted messages to the client if the disconnection occurs.

console.log('disconnect : ' + socket.id); on server showing disconnect with the socket.id.

I'm using socket.io v2.0.4 both on server and client.

How can I send data from the server if a socket disconnect event occurs?

Upvotes: 1

Views: 1052

Answers (1)

im_tsm
im_tsm

Reputation: 2051

Event: disconnect is fired upon disconnection. This simply means, for some reason, there is no connection between the client and the server. So in this situation, you cannot send data to the client as the client is not connected. Disconnection may happen if the client's network is not available(read Internet failure) or the client environment(ex: browser) crashed or the user closed the tab.

Upvotes: 1

Related Questions