Skip to content

Commit 651e0a4

Browse files
committed
Update documentation
Clarify ability to use @MessageMapping methods on both @controller as well as @RestController. Add section on configuring connections (including credentials) to the message broker and clarify the use of the login/passcode headerers of the STOMP CONNECT frame. Add note on when to add the reactor-tcp dependency. Issue: SPR-11464, SPR-11436, SPR-11449
1 parent ce0d916 commit 651e0a4

File tree

1 file changed

+83
-21
lines changed

1 file changed

+83
-21
lines changed

src/asciidoc/index.adoc

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36680,7 +36680,7 @@ that some functionality may be exposed over both WebSocket and as a REST API in
3668036680
order to provide clients with alternatives. Furthermore, a REST API call may need
3668136681
to broadcast a message to interested clients connected via WebSocket.
3668236682

36683-
Spring Framework allows `@Controller` classes to have both
36683+
Spring Framework allows `@Controller` and `@RestController` classes to have both
3668436684
HTTP request handling and WebSocket message handling methods.
3668536685
Furthermore, a Spring MVC request handling method, or any application
3668636686
method for that matter, can easily broadcast a message to all interested
@@ -37360,6 +37360,9 @@ https://github.com/sockjs/sockjs-client[sockjs-client]:
3736037360
----
3736137361
var socket = new SockJS("/spring-websocket-portfolio/portfolio");
3736237362
var stompClient = Stomp.over(socket);
37363+
37364+
stompClient.connect({}, function(frame) {
37365+
}
3736337366
----
3736437367

3736537368
Or if connecting via WebSocket (without SockJS):
@@ -37369,8 +37372,15 @@ Or if connecting via WebSocket (without SockJS):
3736937372
----
3737037373
var socket = new WebSocket("/spring-websocket-portfolio/portfolio");
3737137374
var stompClient = Stomp.over(socket);
37375+
37376+
stompClient.connect({}, function(frame) {
37377+
}
3737237378
----
3737337379

37380+
Note that the stompClient above need does not specify a `login` and `passcode` headers.
37381+
Even if it did, they would be ignored, or rather overridden, on the server side. See the
37382+
sections <<websocket-stomp-handle-broker-relay-configure>> and
37383+
<<websocket-stomp-handle-user>> for more information on authentication.
3737437384

3737537385

3737637386
[[websocket-stomp-handle]]
@@ -37525,7 +37535,8 @@ message to all subscribed, connected clients.
3752537535
[[websocket-stomp-handle-annotations]]
3752637536
===== Annotation-based Message Handling
3752737537

37528-
The `@MessageMapping` annotation is supported on methods of `@Controller`-annotated classes.
37538+
The `@MessageMapping` annotation is supported on methods of `@Controller`
37539+
as well as on `@RestController`-annotated classes.
3752937540
It can be used for mapping methods to path-like message destinations. It is also
3753037541
possible to combine with a type-level `@MessageMapping` for expressing shared
3753137542
mappings across all annotated methods within a controller.
@@ -37628,21 +37639,16 @@ to Ant-style destination patterns.
3762837639

3762937640
The simple broker is great for getting started but supports only a subset of
3763037641
STOMP commands (e.g. no acks, receipts, etc), relies on a simple message
37631-
sending loop, and is not suitable for clustering.
37632-
37633-
Instead, applications can use a full-featured message broker and use it for
37634-
managing client subscriptions and broadcasting messages; while annotated
37635-
methods can still be used for application processing. In other words, all
37636-
else remains the same, except a full-featured broker replaces the simple
37637-
broker.
37642+
sending loop, and is not suitable for clustering. Instead, applications can
37643+
upgrade to using a full-featured message broker.
3763837644

37639-
Check your message broker STOMP documentation (e.g.
37645+
Check the STOMP documentation for your message broker of choice (e.g.
3764037646
http://www.rabbitmq.com/stomp.html[RabbitMQ],
37641-
http://activemq.apache.org/stomp.html[ActiveMQ]), install and run the broker with
37642-
STOMP support enabled. Then enable the STOMP broker relay in the Spring
37643-
configuration as an alternative to the simple broker.
37647+
http://activemq.apache.org/stomp.html[ActiveMQ], or other), install and run the
37648+
broker with STOMP support enabled. Then enable the STOMP broker relay in the
37649+
Spring configuration instead of the simple broker.
3764437650

37645-
Below is example configuration that enables use of a full-featured broker:
37651+
Below is example configuration that enables a full-featured broker:
3764637652

3764737653
[source,java,indent=0]
3764837654
[subs="verbatim,quotes"]
@@ -37689,17 +37695,73 @@ XML configuration equivalent:
3768937695
</beans>
3769037696
----
3769137697

37692-
The STOMP "broker relay" from the above configuration manages TCP connections
37693-
to the external broker, and forwards messages with matching destination prefixes
37694-
to it. Likewise, any messages received from the external broker are matched and
37695-
routed to connected clients.
37698+
The "STOMP broker relay" in the above configuration is a Spring
37699+
http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/messaging/MessageHandler.html[MessageHandler]
37700+
that handles messages by forwarding them to an external message broker.
37701+
To do so it establishes TCP connections to the broker, forwards all
37702+
messages to it, and reversely forwards all messages received
37703+
from the broker to clients through their WebSocket sessions. Essentially
37704+
it acts as a "relay" forwarding messages in both directions.
37705+
37706+
[NOTE]
37707+
====
37708+
Please add a dependency on `org.projectreactor:reactor-tcp` for TCP connection management.
37709+
====
37710+
37711+
Furthermore, application components (e.g. HTTP request handling methods,
37712+
business services, etc) can also send messages to the broker relay, as described
37713+
in <<websocket-stomp-handle-send>>, in order to broadcast messages to
37714+
subscribed WebSocket clients.
37715+
37716+
In effect, the broker relay enables robust and scalable message broadcasting.
3769637717

37697-
In effect, messages are now broadcast through a full-featured, robust and
37698-
scalable message broker.
37718+
[[websocket-stomp-handle-broker-relay-configure]]
37719+
===== Configuring Connections To The Full-Featured Broker
37720+
37721+
A STOMP broker relay maintains a single "system" TCP connection to the broker.
37722+
This connection is used for messages originating from the server-side application
37723+
only, not for receiving messages. You can configure the STOMP credentials
37724+
for this connection, i.e. the STOMP frame `login` and `passcode` headers. This
37725+
is exposed in both the XML namespace and the Java config as the
37726+
`systemLogin`/`systemPasscode` properties with default values `guest`/`guest`.
37727+
37728+
The STOMP broker relay also creates a separate TCP connection for every connected
37729+
WebSocket client. You can configure the STOMP credentials to use for all TCP
37730+
connections created on behalf of clients. This is exposed in both the XML namespace
37731+
and the Java config as the `clientLogin`/`clientPasscode` properties with default
37732+
values `guest`/`guest`.
37733+
37734+
[NOTE]
37735+
====
37736+
The STOMP broker relay always sets the `login` and `passcode` headers on every CONNECT
37737+
frame it forwards to the broker on behalf of clients. Therefore WebSocket clients
37738+
need not set those headers, they will be ignored. As the following section explains
37739+
instead WebSocket clients should rely on HTTP authentication to protect the WebSocket
37740+
endpoint and establish the client identity.
37741+
====
37742+
37743+
The STOMP broker relay also sends and receives heartbeats to and from the message
37744+
broker over the "system" TCP connection. You can configure the intervals for sending
37745+
and receiving heartbeats (10 seconds each by default). If connectivity to the broker
37746+
is lost, the broker relay will continue to try to reconnect, every 5 seconds,
37747+
until it succeeds.
37748+
37749+
[NOTE]
37750+
====
37751+
A Spring bean can implement `ApplicationListener<BrokerAvailabilityEvent>` in order
37752+
to receive notifications when the "system" connection to the broker is lost and
37753+
re-established. For example a Stock Quote service broadcasting stock quotes can
37754+
stop trying to send messages when there is no active "system" connection.
37755+
====
3769937756

37757+
The STOMP broker relay can also be configured with a `virtualHost` property.
37758+
The value of this property will be set as the `host` header of every CONNECT frame
37759+
and may be useful for example in a cloud environment where the actual host to which
37760+
the TCP connection is established is different from the host providing the
37761+
cloud-based STOMP service.
3770037762

3770137763
[[websocket-stomp-handle-user]]
37702-
===== Handling Messages to User Destinations
37764+
===== Authentication and Handling Messages to User Destinations
3770337765

3770437766
An application can also send messages targeting a specific user.
3770537767

0 commit comments

Comments
 (0)