gj / fastify-ws

MIT License
55 stars 11 forks source link

No way to listen for websocket disconnect #4

Closed marchaos closed 6 years ago

marchaos commented 6 years ago

Hey,

I've tried to listen for when a websocket disconnects using:

 ws.on('connection', (socket) => {
       console.info("Someone connected!");
            socket.on('message', (data) => {
       });

       socket.on('disconnect', () => {
            console.info("someone disconnected");
       });
});

disconnect never gets called. Is there a way to know when someone disconnects?

gj commented 6 years ago

Hey there @marchaos !

Try listening for the close event:

ws.on('connection', (socket) => {
  console.info('Someone connected!')

  socket.on('message', (data) => {
    // Handle messages
  })

  socket.on('close', () => {
    console.info('Someone disconnected.')
  })
})
marchaos commented 6 years ago

Hey @gj. Nice one, that works.