The English version of is the official project site. Translated sites are community supported on a best-effort basis.

Using WebSockets with Reactive Messaging

This guide demonstrates how your Quarkus application can utilize SmallRye Reactive Messaging to consume and produce messages via WebSockets. WebSockets support is a part of the Reactive Messaging HTTP extension (quarkus-reactive-messaging-http).


To complete this guide, you need:

  • less than 15 minutes

  • an IDE

  • JDK 11+ installed with JAVA_HOME configured appropriately

  • Apache Maven 3.8.1+

  • GraalVM, Docker or Podman installed if you want to run in native mode.


In this guide we will implement a service, namely CostConverter that consumes messages with costs in multiple currencies and converts each cost to its value in Euro.

To let a user easily try out the service, we will implement an HTTP resource summing up the costs (CostCollector), and a simple web page to add new costs and watch the sum.



We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Clone the Git repository: git clone, or download an archive.

The solution is located in the reactive-messaging-websockets-quickstart directory.

Creating the Maven Project

First, we need a new project. Create a new project with the following command:

mvn io.quarkus:quarkus-maven-plugin:2.11.2.Final:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=reactive-messaging-websockets-quickstart \
    -Dextensions="reactive-messaging-http,resteasy-reactive-jackson" \
cd reactive-messaging-websockets-quickstart

This command generates a Maven project with Reactive Messaging HTTP connector and RESTEasy Reactive with Jackson support extensions.

The Converter

Create the src/main/java/org/acme/reactivews/ file, with the following content:

package org.acme.reactivews;

import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Outgoing;

import javax.enterprise.context.ApplicationScoped;
import java.util.HashMap;
import java.util.Map;

 * A bean consuming costs in multiple currencies and producing prices in EUR from them
public class CostConverter {

    private static final Map<String, Double> conversionRatios = new HashMap<>();

    static {
        conversionRatios.put("CHF", 0.93);
        conversionRatios.put("USD", 0.84);
        conversionRatios.put("PLN", 0.22);
        conversionRatios.put("EUR", 1.0);

    @Incoming("incoming-costs") (1)
    @Outgoing("outgoing-costs") (2)
    Cost convert(Cost cost) { (3)
        Double conversionRatio = conversionRatios.get(cost.getCurrency().toUpperCase());
        if (conversionRatio == null) {
            return cost;
        return new Cost(conversionRatio * cost.getValue(), "EUR");
1 Consume messages from the incoming-costs stream.
2 Dispatch returned values to the outgoing-costs stream.
3 Consume an event with payload of type Cost and produce another Cost, with value converted to Euro if we know the conversion ratio. In the case of consuming an arbitrary object, the reactive-messaging-http extension will attempt to deserialize the request body as a JSON object.
Unlike the outbound HTTP connector, the outbound WebSocket connector does not check if the message is consumed by the remote endpoint. It may happen that a failure to receive a message is not reported, e.g. if the remote side closes the WebSocket connection in a crucial moment.

Let’s define the Cost class:

package org.acme.reactivews;

public class Cost {
    private double value;
    private String currency;

    public Cost(double value, String currency) {
        this.value = value;
        this.currency = currency;

    public Cost() {

    public void setValue(double value) {
        this.value = value;

    public void setCurrency(String currency) {
        this.currency = currency;

    public double getValue() {
        return value;

    public String getCurrency() {
        return currency;

In the next step, we will create configurations for both streams in the file.

Configuring the Web Socket connector

We need to configure the Web Socket connector. This is done in the file. The keys are structured as follows:


The channel-name segment must match the value set in the @Incoming and @Outgoing annotation:

  • incoming-costs → an inbound WebSocket that receives costs

  • outgoing-costs → an outbound WebSocket that pushes converted costs


# the WebSockets are exposed on the same port as HTTP
# for non-test profiles, it is quarkus.http.port...

# for the test profile it is quarkus.http.test-port${quarkus.http.test-port}/cost-collector

# the incoming-costs channel will be fed via a Web Socket endpoint on the `/costs` path


The CostCollector

To illustrate that converting messages and passing them through works, let’s add a CostCollector class that consumes the Web Socket messages and exposes information about the sum of collected costs via REST:

package org.acme.reactivews;

import org.eclipse.microprofile.reactive.messaging.Incoming;

import javax.enterprise.context.ApplicationScoped;

public class CostCollector {
    private double sum;

    // expose the sum of the collected costs
    public synchronized double getCosts() {
        return sum;

    // consume costs from collector channel
    synchronized void collect(Cost cost) {
        if ("EUR".equals(cost.getCurrency())) {
            sum += cost.getValue();

One more thing we have to do is to configure the collector channel in


The HTML page

To conveniently interact with the application, let’s create a simple web page.

The page will provide a form to add costs, and an info of the current sum of costs. The page periodically updates the sum by requesting the current sum from /cost-collector.

Change the src/main/resources/META-INF/resources/index.html file, with the following content:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">

    <link rel="stylesheet" type="text/css"
    <link rel="stylesheet" type="text/css"
<div class="container">

    <h2>Add a cost</h2>
            <label for="value">Value</label>
            <input type="text" id="value">
            <label for="currency">Currency</label>
            <select id="currency">
                <option value="CHF">Swiss franc</option>
                <option value="USD">United States dollar</option>
                <option value="PLN">Polish złoty</option>
                <option value="EUR">Euro</option>
        <input type="button" onclick="add()" value="Add">

    <div class="row">
        <p class="col-md-12">The total cost is <strong><span id="content">N/A</span>&nbsp;&euro;</strong>.</p>
    var webSocket = new WebSocket(`ws://${}/costs`);
    add = function() {
        const cost = {
            value: document.getElementById('value').value,
            currency: document.getElementById('currency').value


    updateCost = function() {
        fetch('collected-costs').then(response => response.text()).then(sum =>

    window.setInterval(updateCost, 500);



./mvnw quarkus:dev

在您的浏览器中打开 http://localhost:8080/index.html



./mvnw package -Pnative



All quarkus-websocket connector options:


# The target URL

# Message serializer, optional, implementation of `io.quarkus.reactivemessaging.http.runtime.serializers.Serializer`

# The number of retries to make for sending a message to a remote websocket endpoint.
# A value greater than 0 is advised. Otherwise, a web socket timeout can result in a dropped message
# The default value is 1

# Configures the random factor when using back-off with maxAttempts > 1, 0.5 by default

# Configures a back-off delay between attempts to send a request.
# A random factor (jitter) is applied to increase the delay when several failures happen.


# The path of the endpoint

# Web socket endpoint buffers messages if a consumer is not able to keep up.
# This setting specifies the size of the buffer. 8 by default


此扩展利用 SmallRye 响应式消息来构建数据流应用程序。

如果您想进一步了解文档 SmallRye Reactive Messaging, 在Quarkus中使用的实现。