Skip to content

Commit

Permalink
Update STOMP WebSocket transport reference docs
Browse files Browse the repository at this point in the history
Closes gh-31616
  • Loading branch information
rstoyanchev committed Nov 22, 2023
1 parent 2784410 commit 1f19bb2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
48 changes: 21 additions & 27 deletions framework-docs/modules/ROOT/pages/web/websocket/server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -229,34 +229,27 @@ Java initialization API. The following example shows how to do so:


[[websocket-server-runtime-configuration]]
== Server Configuration
== Configuring the Server
[.small]#xref:web/webflux-websocket.adoc#webflux-websocket-server-config[See equivalent in the Reactive stack]#

Each underlying WebSocket engine exposes configuration properties that control
runtime characteristics, such as the size of message buffer sizes, idle timeout,
and others.
You can configure of the underlying WebSocket server such as input message buffer size,
idle timeout, and more.

For Tomcat, WildFly, and GlassFish, you can add a `ServletServerContainerFactoryBean` to your
WebSocket Java config, as the following example shows:
For Jakarta WebSocket servers, you can add a `ServletServerContainerFactoryBean` to your
Java configuration. For example:

[source,java,indent=0,subs="verbatim,quotes"]
----
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(8192);
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
}
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(8192);
container.setMaxBinaryMessageBufferSize(8192);
return container;
}
----

The following example shows the XML configuration equivalent of the preceding example:
Or to your XML configuration:

[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
Expand All @@ -277,12 +270,11 @@ The following example shows the XML configuration equivalent of the preceding ex
</beans>
----

NOTE: For client-side WebSocket configuration, you should use `WebSocketContainerFactoryBean`
(XML) or `ContainerProvider.getWebSocketContainer()` (Java configuration).
NOTE: For client Jakarta WebSocket configuration, use
ContainerProvider.getWebSocketContainer() in Java configuration, or
`WebSocketContainerFactoryBean` in XML.

For Jetty, you need to supply a pre-configured Jetty `WebSocketServerFactory` and plug
that into Spring's `DefaultHandshakeHandler` through your WebSocket Java config.
The following example shows how to do so:
For Jetty, you can supply a `Consumer` callback to configure the WebSocket server:

[source,java,indent=0,subs="verbatim,quotes"]
----
Expand All @@ -298,11 +290,9 @@ The following example shows how to do so:
@Bean
public DefaultHandshakeHandler handshakeHandler() {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
policy.setInputBufferSize(8192);
policy.setIdleTimeout(600000);
return new DefaultHandshakeHandler(
new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy)));
}
Expand Down Expand Up @@ -349,6 +339,10 @@ The following example shows the XML configuration equivalent of the preceding ex
</beans>
----

TIP: When using STOMP over WebSocket, you will also need to configure
xref:web/websocket/stomp/server-config.adoc[STOMP WebSocket transport]
properties.



[[websocket-server-allowed-origins]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
[[websocket-stomp-server-config]]
= WebSocket Server
= WebSocket Transport

To configure the underlying WebSocket server, the information in
xref:web/websocket/server.adoc#websocket-server-runtime-configuration[Server Configuration] applies. For Jetty, however you need to set
the `HandshakeHandler` and `WebSocketPolicy` through the `StompEndpointRegistry`:
This section explains how to configure the underlying WebSocket server transport.

For Jakarta WebSocket servers, add a `ServletServerContainerFactoryBean` to your
configuration. For examples, see
xref:web/websocket/server.adoc#websocket-server-runtime-configuration[Configuring the Server]
under the WebSocket section.

For Jetty WebSocket servers, customize the `JettyRequestUpgradeStrategy` as follows:

[source,java,indent=0,subs="verbatim,quotes"]
----
Expand All @@ -29,5 +34,20 @@ the `HandshakeHandler` and `WebSocketPolicy` through the `StompEndpointRegistry`
}
----

In addition to WebSocket server properties, there are also STOMP WebSocket transport properties
to customize as follows:

[source,java,indent=0,subs="verbatim,quotes"]
----
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
registry.setMessageSizeLimit(4 * 8192);
registry.setTimeToFirstMessage(30000);
}
}
----

0 comments on commit 1f19bb2

Please sign in to comment.