You can inject a reference to the a instance using the @Client annotation:

    This allows you to use the same service discovery and load balancing features for WebSocket clients.

    Once you have a reference to the interface you can use the method to obtain a connected instance of a bean annotated with @ClientWebSocket.

    For example consider the following implementation:

    WebSocket Chat Example

    1. import io.micronaut.http.HttpRequest;
    2. import io.micronaut.websocket.WebSocketSession;
    3. import io.micronaut.websocket.annotation.ClientWebSocket;
    4. import io.micronaut.websocket.annotation.OnMessage;
    5. import io.micronaut.websocket.annotation.OnOpen;
    6. import io.reactivex.Single;
    7. import java.util.Collection;
    8. import java.util.concurrent.ConcurrentLinkedQueue;
    9. import java.util.concurrent.Future;
    10. @ClientWebSocket("/chat/{topic}/{username}") (1)
    11. public abstract class ChatClientWebSocket implements AutoCloseable { (2)
    12. private WebSocketSession session;
    13. private HttpRequest request;
    14. private String topic;
    15. private String username;
    16. private Collection<String> replies = new ConcurrentLinkedQueue<>();
    17. @OnOpen
    18. public void onOpen(String topic, String username, WebSocketSession session, HttpRequest request) { (3)
    19. this.topic = topic;
    20. this.username = username;
    21. this.session = session;
    22. this.request = request;
    23. }
    24. public String getTopic() {
    25. return topic;
    26. }
    27. public String getUsername() {
    28. return username;
    29. }
    30. return replies;
    31. }
    32. public WebSocketSession getSession() {
    33. return session;
    34. }
    35. public HttpRequest getRequest() {
    36. return request;
    37. }
    38. @OnMessage
    39. public void onMessage(
    40. String message) {
    41. replies.add(message); (4)
    42. }
    1. import io.micronaut.http.HttpRequest
    2. import io.micronaut.websocket.WebSocketSession
    3. import io.micronaut.websocket.annotation.ClientWebSocket
    4. import io.micronaut.websocket.annotation.OnMessage
    5. import io.micronaut.websocket.annotation.OnOpen
    6. import io.reactivex.Single
    7. import java.util.concurrent.ConcurrentLinkedQueue
    8. import java.util.concurrent.Future
    9. @ClientWebSocket("/chat/{topic}/{username}") (1)
    10. abstract class ChatClientWebSocket implements AutoCloseable { (2)
    11. private WebSocketSession session
    12. private HttpRequest request
    13. private String topic
    14. private String username
    15. private Collection<String> replies = new ConcurrentLinkedQueue<>()
    16. @OnOpen
    17. this.topic = topic
    18. this.username = username
    19. this.session = session
    20. this.request = request
    21. }
    22. String getTopic() {
    23. topic
    24. }
    25. String getUsername() {
    26. username
    27. }
    28. Collection<String> getReplies() {
    29. replies
    30. }
    31. WebSocketSession getSession() {
    32. session
    33. }
    34. HttpRequest getRequest() {
    35. request
    36. }
    37. @OnMessage
    38. void onMessage(
    39. String message) {
    40. replies.add(message) (4)
    41. }

    WebSocket Chat Example

    You can also define abstract methods that start with either send or broadcast and these methods will be implemented for you at compile time. For example:

    WebSocket Send Methods

    1. public abstract void send(String message);

    Note by returning void this tells Micronaut that the method is a blocking send. You can instead define methods that return either futures or a :

    WebSocket Send Methods

    1. public abstract io.reactivex.Single<String> send(String message);

    WebSocket Send Methods

    The above example defines a send method that executes asynchronously and returns a Future to access the results.

    Once you have defined a client class you can connect to the client socket and start sending messages:

    Connecting a Client WebSocket

    1. ChatClientWebSocket chatClient = webSocketClient.connect(ChatClientWebSocket.class, "/chat/football/fred").blockingFirst();
    2. chatClient.send("Hello World!");