From 3352f737b38df6cc420d99987ce7cd1385db90de Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Thu, 16 Mar 2023 09:50:43 +0100 Subject: [PATCH] Docs - RESTEasy Reactive SSE support - document customized message --- docs/src/main/asciidoc/resteasy-reactive.adoc | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/docs/src/main/asciidoc/resteasy-reactive.adoc b/docs/src/main/asciidoc/resteasy-reactive.adoc index afd1743e3d6f9..b466099b6009a 100644 --- a/docs/src/main/asciidoc/resteasy-reactive.adoc +++ b/docs/src/main/asciidoc/resteasy-reactive.adoc @@ -954,7 +954,6 @@ import jakarta.ws.rs.core.MediaType; import org.jboss.resteasy.reactive.RestStreamElementType; import io.smallrye.mutiny.Multi; -import io.smallrye.mutiny.Uni; import io.smallrye.reactive.messaging.annotations.Channel; @@ -976,6 +975,52 @@ public class Endpoint { } ---- +Sometimes it's useful to create a customized SSE message, for example if you need to specify the `event` field of a SSE message to distinguish various event types. +A resource method may return `Multi` and an injected `jakarta.ws.rs.sse.Sse` can be used to create `OutboundSseEvent` instances. + +[source,java] +---- +package org.acme.rest; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.sse.OutboundSseEvent; +import jakarta.ws.rs.sse.Sse; + +import org.jboss.resteasy.reactive.RestStreamElementType; + +import io.smallrye.mutiny.Multi; + +import io.smallrye.reactive.messaging.annotations.Channel; + +@Path("escoffier") +public class Endpoint { + + @Inject + @Channel("book-out") + Multi books; + + @Inject + Sse sse; <1> + + @GET + @RestStreamElementType(MediaType.TEXT_PLAIN) + public Multi stream() { + return books.map(book -> sse.newEventBuilder() <2> + .name("book") <3> + .data(book.title) <4> + .build()); + } +} +---- +<1> Inject the server-side entry point for creating ``OutboundSseEvent``s. +<2> Create a new outbound event builder. +<3> Set the event name, i.e. the value of the `event` field of a SSE message. +<4> Set the data, i.e. the value of the `data` field of a SSE message. + === Controlling HTTP Caching features RESTEasy Reactive provides the link:{resteasy-reactive-common-api}/org/jboss/resteasy/reactive/Cache.html[`@Cache`]