Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an event for client disconnect of namespace on server #46

Closed
altjz opened this issue Aug 27, 2019 · 2 comments
Closed

Add an event for client disconnect of namespace on server #46

altjz opened this issue Aug 27, 2019 · 2 comments
Assignees

Comments

@altjz
Copy link

altjz commented Aug 27, 2019

Great thanks to this nice library.

I need to do some cleanup job after client disconnect from server. Is it possible to emit an event after client disconnect?

server.on('disconnect', (socket, request) => {
  // do something cleanup job
});

or

namespace.on('disconnect', (socket, request) => {
  // do something cleanup job
});
@mkozjak
Copy link
Member

mkozjak commented Aug 27, 2019

Hi, @altjz! Thanks!

Please use server's connection event to get the socket and set events on it.
https://github.com/elpheria/rpc-websockets/blob/master/API.md#event-connection

For example:

server.on('connection', (socket, request) => {
    socket.on('close', () => {
        // do some cleanup job
    })
})

websockets/ws is the backend library underneath rpc-websockets, so this applies:
https://github.com/websockets/ws/blob/master/doc/ws.md#event-close-1

@mkozjak mkozjak closed this as completed Aug 27, 2019
@mkozjak mkozjak self-assigned this Aug 27, 2019
@altjz
Copy link
Author

altjz commented Aug 27, 2019

thanks @mkozjak for quick response.
I tried this before, but is it possible to do some cleanup job after all sockets disconnect from specific namespace?

server.on("connection", (socket, request) => {
    socket.on("close", () =>
    {
        const u = url.parse(request.url, true);
        const ns = u.pathname;
        console.log('client.size: ', server.namespaces[ns].clients.size); // this won't be 0
        if (server.namespaces[ns].clients.size === 0) {
            console.log('clean up');
        }
    })
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants