@@ -36680,7 +36680,7 @@ that some functionality may be exposed over both WebSocket and as a REST API in
36680
36680
order to provide clients with alternatives. Furthermore, a REST API call may need
36681
36681
to broadcast a message to interested clients connected via WebSocket.
36682
36682
36683
- Spring Framework allows `@Controller` classes to have both
36683
+ Spring Framework allows `@Controller` and `@RestController` classes to have both
36684
36684
HTTP request handling and WebSocket message handling methods.
36685
36685
Furthermore, a Spring MVC request handling method, or any application
36686
36686
method for that matter, can easily broadcast a message to all interested
@@ -37360,6 +37360,9 @@ https://github.com/sockjs/sockjs-client[sockjs-client]:
37360
37360
----
37361
37361
var socket = new SockJS("/spring-websocket-portfolio/portfolio");
37362
37362
var stompClient = Stomp.over(socket);
37363
+
37364
+ stompClient.connect({}, function(frame) {
37365
+ }
37363
37366
----
37364
37367
37365
37368
Or if connecting via WebSocket (without SockJS):
@@ -37369,8 +37372,15 @@ Or if connecting via WebSocket (without SockJS):
37369
37372
----
37370
37373
var socket = new WebSocket("/spring-websocket-portfolio/portfolio");
37371
37374
var stompClient = Stomp.over(socket);
37375
+
37376
+ stompClient.connect({}, function(frame) {
37377
+ }
37372
37378
----
37373
37379
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.
37374
37384
37375
37385
37376
37386
[[websocket-stomp-handle]]
@@ -37525,7 +37535,8 @@ message to all subscribed, connected clients.
37525
37535
[[websocket-stomp-handle-annotations]]
37526
37536
===== Annotation-based Message Handling
37527
37537
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.
37529
37540
It can be used for mapping methods to path-like message destinations. It is also
37530
37541
possible to combine with a type-level `@MessageMapping` for expressing shared
37531
37542
mappings across all annotated methods within a controller.
@@ -37628,21 +37639,16 @@ to Ant-style destination patterns.
37628
37639
37629
37640
The simple broker is great for getting started but supports only a subset of
37630
37641
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.
37638
37644
37639
- Check your message broker STOMP documentation (e.g.
37645
+ Check the STOMP documentation for your message broker of choice (e.g.
37640
37646
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.
37644
37650
37645
- Below is example configuration that enables use of a full-featured broker:
37651
+ Below is example configuration that enables a full-featured broker:
37646
37652
37647
37653
[source,java,indent=0]
37648
37654
[subs="verbatim,quotes"]
@@ -37689,17 +37695,73 @@ XML configuration equivalent:
37689
37695
</beans>
37690
37696
----
37691
37697
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.
37696
37717
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
+ ====
37699
37756
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.
37700
37762
37701
37763
[[websocket-stomp-handle-user]]
37702
- ===== Handling Messages to User Destinations
37764
+ ===== Authentication and Handling Messages to User Destinations
37703
37765
37704
37766
An application can also send messages targeting a specific user.
37705
37767
0 commit comments