moquette icon indicating copy to clipboard operation
moquette copied to clipboard

How to forcefully disconnect the client from the broker?

Open giri110890 opened this issue 5 years ago • 9 comments

Expected behavior

In Version 0.11, we could use the below code snippet to disconnect the client forcefully from the broker.

public boolean disconnectClient(String endpoint) { ConnectionDescriptor connectionDescriptor = mqttBroker.getConnectionsManager().getConnection(endpoint); mqttBroker.getConnectionsManager().getConnection(endpoint).abort(); boolean status = mqttBroker.getConnectionsManager().removeConnection(connectionDescriptor); return status; }

Server.java exposed the API to get the ConnectionsManager through which we were able to abort the connection.

public IConnectionsManager getConnectionsManager() { return m_processorBootstrapper.getConnectionDescriptors(); }

Actual behavior

As of now in the version 0.12.1, we are unable to find the similar API to abort the client connection. Could you please help us with the information as to how we can disconnect the client forcefully using any other API that's available.

giri110890 avatar Apr 08 '19 12:04 giri110890

Hi @giri110890 I plan to solve it, not giving access to the connection manager or internal data structures (like the train wrecks as before), but with just a simple direct method (for example broker, close(clientId))

andsel avatar Apr 08 '19 14:04 andsel

Hi @andsel Thanks for the quick response. Can you please let me know what's the approximate timeframe when this feature can be implemented?

giri110890 avatar Apr 08 '19 15:04 giri110890

I don't know when I'll have free time to dedicate to this, sorry

On Mon, Apr 8, 2019 at 5:45 PM giri110890 [email protected] wrote:

Hi @andsel https://github.com/andsel Thanks for the quick response. Can you please let me know what's the approximate timeframe when this feature can be implemented?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/moquette-io/moquette/issues/469#issuecomment-480887442, or mute the thread https://github.com/notifications/unsubscribe-auth/AA_Y0ctodk7Er-BsB0Akf2lNnCZyIcatks5ve2QGgaJpZM4ciB-O .

andsel avatar Apr 08 '19 15:04 andsel

any further movement on this?

we really need a mechanism for disconnecting a client gracefully.

abonstu avatar Jun 24 '19 01:06 abonstu

@abonstu as I wrote in the comment of 8th April the solution could be to provide an API as a close(clientId) method at broker level. If you want you could provide a PR

andsel avatar Jun 26 '19 10:06 andsel

Hi there. I have a need for this right now. So, I want to get a PR in for this. I think I see a fairly easy way to do it when exposing a close(clientId) method on the Server class as you mentioned.

This is what I was thinking of doing, but I have a couple of questions.

This is not the exact code I would use, but it should get the idea across.

public void close(String clientId) {
    sessions.disconnect(clientId);
    sessions.remove(clientId);
}

The question I have is that the Session class has a method closeImmediately(), which in turn drops the mqtt connection. I was wondering if I should call this first on the session before calling sessions.disconnect(). The sessions.disconnect() method just sets the mqttConnection to null, but does not call closeImmediately.

Is the code I have sufficient, or should I be closing the mqtt connection before it gets set to null?

Any help is appreciated. I will probably play around with it and answer my own question, but also looking for feedback.

davesters avatar Aug 09 '19 06:08 davesters

sessions.disconnect(cliID) should does a clean disconnect, while the other drop the socket channel

andsel avatar Aug 09 '19 09:08 andsel

Awesome, thanks!

davesters avatar Aug 09 '19 17:08 davesters

So, I had to go with this solution. Just doing sessions.disconnect() was not actually closing the client connection and my client remained connected.

public void close(String clientId) {
        final Session session = sessions.retrieve(clientId);

        if (session != null) {
            session.closeImmediately();
            session.disconnect();
        }
    }

davesters avatar Aug 09 '19 20:08 davesters