John Eipe
John Eipe

Reputation: 11236

STOMP Websocket synchronous communication using Spring

I have a requirement were some of the STOMP websocket connections needs to be handled synchronously.

Meaning I have a client (spring) subscribed to a topic ("/topic").

I have a server (spring) that has defined the broker ("/topic") also defined handlers ("/app/hello").

Now is it possible for the client to send a request to /app/hello and then wait for a response before sending the next request to /app/hello.

  1. How do I return value on my sever (STOMP spec says about RECEIPT frames but I don't think this is something that can be manually controlled).
  2. How do I wait for the value on my client after a send.

Upvotes: 2

Views: 2410

Answers (1)

dpr
dpr

Reputation: 10972

To connect a Java client to a websocket endpoint you can use the tyrus reference implementation of JSR356 - Websockets For Java.

Basically you will need to implement a client endpoint (javax.websocket.Endpoint) and a message handler (javax.websocket.MessageHandler). In the endpoint you register the message handler with the current session on open:

public class ClientEndpoint extends Endpoint {
  ...

  @Override
  public void onOpen(final Session aSession, final EndpointConfig aConfig) {
    aSession.addMessageHandler(yourMessageHandler);
  }
}

To connect to the server endpoint you can use the ClientManager:

final ClientManager clientManager = ClientManager.createClient();
clientManager.connectToServer(clientEndpoint, config, uriToServerEndpoint);

The message handler's onMessage method will be invoked, if the server endpoint sends something to the topic.

Depending on your needs you can either choose to implement the mentioned interfaces or use the corresponding annotations.

UPDATE: The STOMP website lists several implementations of the STOMP protocol. For Java there are Gozirra and Stampy. I have no experience with these frameworks but the examples are pretty straight forward.

Upvotes: 1

Related Questions