Search code examples
springspring-bootstompspring-websocket

Disconnect client session from Spring websocket stomp server


I've searched quite a bit and been unable to find this: Is there a way that a spring websocket stomp server can disconnect a client based on the sessionId (or really based on anything at all)?

It seems to me that once a client connects to a server there is nothing that allows the server to disconnect the client.


Solution

  • As far as I know the API doesn't provide what you are looking for, on server-side you can only detect disconnect events. If you want to disconnect a certain client I think you must go for a litte workaround, e.g. this one:

    1. Write a client-side javascript function that is able to trigger a disconnect
    2. As soon as your client is connected to the server, generate a client ID in your javascript and send it to the server. Remember the ID on the client, you'll need it in step (4).
    3. At the time you want the server to disconnect the connection to the specific client (identified by the ID), send a message containing the ID back to the client.
    4. Now your client javascript evaluates the message send from the server and decides to call the disconnect function you wrote in step (1).
    5. Your client disconnects itself.

    The workaround is a bit cumbersome but it'll work.