WebSockets in Helidon MP

Santiago Pericas-Geertsen
Helidon
Published in
3 min readJun 25, 2020

Helidon integrates with Tyrus to provide support for the Jakarta WebSocket API . The WebSocket API enables Java applications to participate in WebSocket interactions as servers or clients.

Helidon MP support is centered around annotations and bean discovery using CDI. Developers can choose between annotated and programmatic endpoints or use any combination of them. Using annotated endpoints is recommended in MP as they usually result in more succinct and easier-to-read code.

Project Dependencies

To use the WebSocket extension in your Helidon MP application, simply add the following dependency to your POM file:

Example

This section describes the implementation of a simple application that uses a REST resource to push messages into a shared queue, and a WebSocket endpoint to download messages from the queue, one at a time, over a connection. The example will show how REST and WebSocket connections can be seamlessly combined into a Helidon application.

The Helidon MP application shown here takes full advantage of CDI and class scanning and does not require any additional code given that all the necessary information is available from the code annotations.

The REST endpoint is implemented as a JAX-RS resource, and the queue is directly injected as show next:

There is a single instance ofMessageQueue in application scope that will be shared between the REST and WebSocket endpoints. The REST endpoint pushes messages into the queue and the WebSocket endpoint pulls them out of it.

Let us now explore the implementation of the WebSocket endpoint using the annotated API:

Santiago Pericas-Geertsen
Helidon