web-sockets.md 50.9 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
# WebSockets Support

## WebSockets Support

Starting with version 4.1, Spring Integration has WebSocket support.
It is based on the architecture, infrastructure, and API from the Spring Framework’s `web-socket` module.
Therefore, many of Spring WebSocket’s components (such as `SubProtocolHandler` or `WebSocketClient`) and configuration options (such as `@EnableWebSocketMessageBroker`) can be reused within Spring Integration.
For more information, see the [Spring Framework WebSocket Support](https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#websocket) chapter in the Spring Framework reference manual.

You need to include this dependency into your project:

Maven

```
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-websocket</artifactId>
    <version>5.5.9</version>
</dependency>
```

Gradle

```
compile "org.springframework.integration:spring-integration-websocket:5.5.9"
```

For server side, the `org.springframework:spring-webmvc` dependency must be included explicitly.

The Spring Framework WebSocket infrastructure is based on the Spring messaging foundation and provides a basic messaging framework based on the same `MessageChannel` implementations and `MessageHandler` implementations that Spring Integration uses (and some POJO-method annotation mappings).
Consequently, Spring Integration can be directly involved in a WebSocket flow, even without WebSocket adapters.
For this purpose, you can configure a Spring Integration `@MessagingGateway` with appropriate annotations, as the following example shows:

```
@MessagingGateway
@Controller
public interface WebSocketGateway {

    @MessageMapping("/greeting")
    @SendToUser("/queue/answer")
    @Gateway(requestChannel = "greetingChannel")
    String greeting(String payload);

}
```

### Overview

Since the WebSocket protocol is streaming by definition and we can send and receive messages to and from a WebSocket at the same time, we can deal with an appropriate `WebSocketSession`, regardless of being on the client or server side.
To encapsulate the connection management and `WebSocketSession` registry, the `IntegrationWebSocketContainer` is provided with `ClientWebSocketContainer` and `ServerWebSocketContainer` implementations.
Thanks to the [WebSocket API](https://www.jcp.org/en/jsr/detail?id=356) and its implementation in the Spring Framework (with many extensions), the same classes are used on the server side as well as the client side (from a Java perspective, of course).
Consequently, most connection and `WebSocketSession` registry options are the same on both sides.
That lets us reuse many configuration items and infrastructure hooks to build WebSocket applications on the server side as well as on the client side.
The following example shows how components can serve both purposes:

```
//Client side
@Bean
public WebSocketClient webSocketClient() {
    return new SockJsClient(Collections.singletonList(new WebSocketTransport(new JettyWebSocketClient())));
}

@Bean
public IntegrationWebSocketContainer clientWebSocketContainer() {
    return new ClientWebSocketContainer(webSocketClient(), "ws://my.server.com/endpoint");
}

//Server side
@Bean
public IntegrationWebSocketContainer serverWebSocketContainer() {
    return new ServerWebSocketContainer("/endpoint").withSockJs();
}
```

The `IntegrationWebSocketContainer` is designed to achieve bidirectional messaging and can be shared between inbound and outbound channel adapters (see below), can be referenced from only one of them when using one-way (sending or receiving) WebSocket messaging.
It can be used without any channel adapter, but, in this case, `IntegrationWebSocketContainer` only plays a role as the `WebSocketSession` registry.

|   |The `ServerWebSocketContainer` implements `WebSocketConfigurer` to register an internal `IntegrationWebSocketContainer.IntegrationWebSocketHandler` as an `Endpoint`.<br/>It does so under the provided `paths` and other server WebSocket options (such as `HandshakeHandler` or `SockJS fallback`) within the `ServletWebSocketHandlerRegistry` for the target vendor WebSocket Container.<br/>This registration is achieved with an infrastructural `WebSocketIntegrationConfigurationInitializer` component, which does the same as the `@EnableWebSocket` annotation.<br/>This means that, by using `@EnableIntegration` (or any Spring Integration namespace in the application context), you can omit the `@EnableWebSocket` declaration, because the Spring Integration infrastructure detects all WebSocket endpoints.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

### WebSocket Inbound Channel Adapter

The `WebSocketInboundChannelAdapter` implements the receiving part of `WebSocketSession` interaction.
You must supply it with a `IntegrationWebSocketContainer`, and the adapter registers itself as a `WebSocketListener` to handle incoming messages and `WebSocketSession` events.

|   |Only one `WebSocketListener` can be registered in the `IntegrationWebSocketContainer`.|
|---|--------------------------------------------------------------------------------------|

For WebSocket subprotocols, the `WebSocketInboundChannelAdapter` can be configured with `SubProtocolHandlerRegistry` as the second constructor argument.
The adapter delegates to the `SubProtocolHandlerRegistry` to determine the appropriate `SubProtocolHandler` for the accepted `WebSocketSession` and to convert a `WebSocketMessage` to a `Message` according to the sub-protocol implementation.

|   |By default, the `WebSocketInboundChannelAdapter` relies only on the raw `PassThruSubProtocolHandler` implementation, which converts the `WebSocketMessage` to a `Message`.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

The `WebSocketInboundChannelAdapter` accepts and sends to the underlying integration flow only `Message` instances that have `SimpMessageType.MESSAGE` or an empty `simpMessageType` header.
All other `Message` types are handled through the `ApplicationEvent` instances emitted from a `SubProtocolHandler` implementation (such as `StompSubProtocolHandler`).

On the server side, if the `@EnableWebSocketMessageBroker` configuration is present, you can configure `WebSocketInboundChannelAdapter` with the `useBroker = true` option.
In this case, all `non-MESSAGE` `Message` types are delegated to the provided `AbstractBrokerMessageHandler`.
In addition, if the broker relay is configured with destination prefixes, those messages that match the Broker destinations are routed to the `AbstractBrokerMessageHandler` instead of to the `outputChannel` of the `WebSocketInboundChannelAdapter`.

If `useBroker = false` and the received message is of the `SimpMessageType.CONNECT` type, the `WebSocketInboundChannelAdapter` immediately sends a `SimpMessageType.CONNECT_ACK` message to the `WebSocketSession` without sending it to the channel.

|   |Spring’s WebSocket Support allows the configuration of only one broker relay.<br/>Consequently, we do not require an `AbstractBrokerMessageHandler` reference.<br/>It is detected in the Application Context.|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

For more configuration options, see [WebSockets Namespace Support](#web-sockets-namespace).

### WebSocket Outbound Channel Adapter

The `WebSocketOutboundChannelAdapter`:

1. Accepts Spring Integration messages from its `MessageChannel`

2. Determines the `WebSocketSession` `id` from the `MessageHeaders`

3. Retrieves the `WebSocketSession` from the provided `IntegrationWebSocketContainer`

4. Delegates the conversion and sending of `WebSocketMessage` work to the appropriate `SubProtocolHandler` from the provided `SubProtocolHandlerRegistry`.

On the client side, the `WebSocketSession` `id` message header is not required, because `ClientWebSocketContainer` deals only with a single connection and its `WebSocketSession` respectively.

To use the STOMP sub-protocol, you should configure this adapter with a `StompSubProtocolHandler`.
Then you can send any STOMP message type to this adapter, using `StompHeaderAccessor.create(StompCommand…​)` and a `MessageBuilder`, or just using a `HeaderEnricher` (see [Header Enricher](./content-enrichment.html#header-enricher)).

The rest of this chapter covers largely additional configuration options.

### WebSockets Namespace Support

The Spring Integration WebSocket namespace includes several components described in the remainder of this chapter.
To include it in your configuration, use the following namespace declaration in your application context configuration file:

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:int="http://www.springframework.org/schema/integration"
  xmlns:int-websocket="http://www.springframework.org/schema/integration/websocket"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration
    https://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/websocket
    https://www.springframework.org/schema/integration/websocket/spring-integration-websocket.xsd">
    ...
</beans>
```

#### `<int-websocket:client-container>` Attributes

The following listing shows the attributes available for the `<int-websocket:client-container>` element:

```
<int-websocket:client-container
                  id=""                        (1)
                  client=""                    (2)
                  uri=""                       (3)
                  uri-variables=""             (4)
                  origin=""                    (5)
                  send-time-limit=""           (6)
                  send-buffer-size-limit=""    (7)
                  auto-startup=""              (8)
                  phase="">                    (9)
                <int-websocket:http-headers>
                  <entry key="" value=""/>
                </int-websocket:http-headers>  (10)
</int-websocket:client-container>
```

|**1** |                                                                                                                                                                            The component bean name.                                                                                                                                                                            |
|------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|**2** |                                                                                                                                                                     The `WebSocketClient` bean reference.                                                                                                                                                                      |
|**3** |                                                                                                  The `uri` or `uriTemplate` to the target WebSocket service.<br/>If you use it as a `uriTemplate` with URI variable placeholders, the `uri-variables` attribute is required.                                                                                                   |
|**4** |Comma-separated values for the URI variable placeholders within the `uri` attribute value.<br/>The values are replaced into the placeholders according to their order in the `uri`.<br/>See [`UriComponents.expand(Object…​uriVariableValues)`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/UriComponents.html#expand-java.lang.Object).|
|**5** |                                                                                                                                                                   The `Origin` Handshake HTTP header value.                                                                                                                                                                    |
|**6** |                                                                                                                                                      The WebSocket session 'send' timeout limit.<br/>Defaults to `10000`.                                                                                                                                                      |
|**7** |                                                                                                                                                   The WebSocket session 'send' message size limit.<br/>Defaults to `524288`.                                                                                                                                                   |
|**8** |                                                                                 Boolean value indicating whether this endpoint should start automatically.<br/>Defaults to `false`, assuming that this container is started from the [WebSocket inbound adapter](#web-socket-inbound-adapter).                                                                                 |
|**9** |        The lifecycle phase within which this endpoint should start and stop.<br/>The lower the value, the earlier this endpoint starts and the later it stops.<br/>The default is `Integer.MAX_VALUE`.<br/>Values can be negative.<br/>See [`SmartLifeCycle`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/SmartLifecycle.html).         |
|**10**|                                                                                                                                                        A `Map` of `HttpHeaders` to be used with the Handshake request.                                                                                                                                                         |

#### `<int-websocket:server-container>` Attributes

The following listing shows the attributes available for the `<int-websocket:server-container>` element:

```
<int-websocket:server-container
          id=""                         (1)
          path=""                       (2)
          handshake-handler=""          (3)
          handshake-interceptors=""     (4)
          decorator-factories=""        (5)
          send-time-limit=""            (6)
          send-buffer-size-limit=""     (7)
          allowed-origins="">           (8)
          <int-websocket:sockjs
            client-library-url=""       (9)
            stream-bytes-limit=""       (10)
            session-cookie-needed=""    (11)
            heartbeat-time=""           (12)
            disconnect-delay=""         (13)
            message-cache-size=""       (14)
            websocket-enabled=""        (15)
            scheduler=""                (16)
            message-codec=""            (17)
            transport-handlers=""       (18)
            suppress-cors="true"="" />  (19)
</int-websocket:server-container>
```

|**1** |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              The component bean name.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|**2** |                                                                                                                                                                                                                                                                                                                                                                                                                       A path (or comma-separated paths) that maps a particular request to a `WebSocketHandler`.<br/>Supports exact path mapping URIs (such as `/myPath`) and ant-style path patterns (such as `/myPath/**`).                                                                                                                                                                                                                                                                                                                                                                                                                       |
|**3** |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 The `HandshakeHandler` bean reference.<br/>Defaults to `DefaultHandshakeHandler`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|**4** |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  List of `HandshakeInterceptor` bean references.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|**5** |                                                                                                                                                                                                                                                                                           List of one or more factories (`WebSocketHandlerDecoratorFactory`) that decorate the handler used to process WebSocket messages.<br/>This may be useful for some advanced use cases (for example, to allow Spring Security to forcibly close<br/>the WebSocket session when the corresponding HTTP session expires).<br/>See the [Spring Session Project](https://docs.spring.io/spring-session/docs/current/reference/html5/#websocket) for more information.                                                                                                                                                                                                                                                                                           |
|**6** |                                                                                                                                                                                                                                                                                                                                                                                                                                                                      See the same option on the [`<int-websocket:client-container>`](#websocket-client-container-attributes).                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
|**7** |                                                                                                                                                                                                                                                                                                                                                                                                                                                                      See the same option on the [`<int-websocket:client-container>`](#websocket-client-container-attributes).                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
|**8** |                                                                                                                                                                                                The allowed origin header values.<br/>You can specify multiple origins as a comma-separated list.<br/>This check is mostly designed for browser clients.<br/>There is nothing preventing other types of client from modifying the origin header value.<br/>When SockJS is enabled and allowed origins are restricted, transport types that do not use origin headers for cross-origin requests (`jsonp-polling`, `iframe-xhr-polling`, `iframe-eventsource`, and `iframe-htmlfile`) are disabled.<br/>As a consequence, IE6 and IE7 are not supported, and IE8 and IE9 are supported only without cookies.<br/>By default, all origins are allowed.                                                                                                                                                                                                 |
|**9** |Transports with no native cross-domain communication (such as `eventsource` and `htmlfile`) must get a simple page from the “foreign” domain in an invisible iframe so that code in the iframe can run from a domain local to the SockJS server.<br/>Since the iframe needs to load the SockJS javascript client library, this property lets you specify the location from which to load it.<br/>By default, it points to `[https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.4.min.js](https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.4.min.js)`.<br/>However, you can also set it to point to a URL served by the application.<br/>Note that it is possible to specify a relative URL, in which case the URL must be relative to the iframe URL.<br/>For example, assuming a SockJS endpoint mapped to `/sockjs` and the resulting iframe URL is `/sockjs/iframe.html`, the relative URL must start with "../../" to traverse up to the location above the SockJS mapping.<br/>For prefix-based servlet mapping, you may need one more traversal.|
|**10**|                                                                                                                                                                                                                                                                                                                                                                                                                                          Minimum number of bytes that can be sent over a single HTTP streaming request before it is closed.<br/>Defaults to `128K` (that is, 128\*1024 or 131072 bytes).                                                                                                                                                                                                                                                                                                                                                                                                                                           |
|**11**|                                                                                                                                                                                                                                                                                                                                                                            The `cookie_needed` value in the response from the SockJs `/info` endpoint.<br/>This property indicates whether a `JSESSIONID` cookie is required for the application to function correctly (for example, for load balancing or in Java Servlet containers for the use of an HTTP session).                                                                                                                                                                                                                                                                                                                                                                             |
|**12**|                                                                                                                                                                                                                                                                                                                                                                                              The amount of time (in milliseconds) when the server has not sent any messages and after which the server should<br/>send a heartbeat frame to the client in order to keep the connection from breaking.<br/>The default value is `25,000` (25 seconds).                                                                                                                                                                                                                                                                                                                                                                                              |
|**13**|                                                                                                                                                                                                                                                                                                                                                                                                    The amount of time (in milliseconds) before a client is considered disconnected after not having a receiving connection (that is, an active connection over which the server can send data to the client).<br/>The default value is `5000`.                                                                                                                                                                                                                                                                                                                                                                                                     |
|**14**|                                                                                                                                                                                                                                                                                                                                                                                                                                          The number of server-to-client messages that a session can cache while waiting for the next HTTP polling request from the client.<br/>The default size is `100`.                                                                                                                                                                                                                                                                                                                                                                                                                                          |
|**15**|                                                                                                                                                                                                                                                                                                                                                                                                                                       Some load balancers do not support WebSockets.<br/>Set this option to `false` to disable the WebSocket transport on the server side.<br/>The default value is `true`.                                                                                                                                                                                                                                                                                                                                                                                                                                        |
|**16**|                                                                                                                                                                                                                                                                                                                                                                                                                            The `TaskScheduler` bean reference.<br/>A new `ThreadPoolTaskScheduler` instance is created if no value is provided.<br/>This scheduler instance is used for scheduling heart-beat messages.                                                                                                                                                                                                                                                                                                                                                                                                                            |
|**17**|                                                                                                                                                                                                                                                                                                                                                                                                                 The `SockJsMessageCodec` bean reference to use for encoding and decoding SockJS messages.<br/>By default, `Jackson2SockJsMessageCodec` is used, which requires the Jackson library to be present on the classpath.                                                                                                                                                                                                                                                                                                                                                                                                                 |
|**18**|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    List of `TransportHandler` bean references.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
|**19**|                                                                                                                                                                                                                                                                                                                                                                                                                                                                    Whether to disable automatic addition of CORS headers for SockJS requests.<br/>The default value is `false`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |

#### `<int-websocket:outbound-channel-adapter>` Attributes

The following listing shows the attributes available for the `<int-websocket:outbound-channel-adapter>` element:

```
<int-websocket:outbound-channel-adapter
                          id=""                             (1)
                          channel=""                        (2)
                          container=""                      (3)
                          default-protocol-handler=""       (4)
                          protocol-handlers=""              (5)
                          message-converters=""             (6)
                          merge-with-default-converters=""  (7)
                          auto-startup=""                   (8)
                          phase=""/>                        (9)
```

|**1**|                                       The component bean name.<br/>If you do not provide the `channel` attribute, a `DirectChannel` is created and registered in the application context with this `id` attribute as the bean name.<br/>In this case, the endpoint is registered with the bean name `id` plus `.adapter`.<br/>And the `MessageHandler` is registered with the bean alias `id` plus `.handler`.                                       |
|-----|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|**2**|                                                                                                                                                                                                   Identifies the channel attached to this adapter.                                                                                                                                                                                                   |
|**3**|                                                                                                                                           The reference to the `IntegrationWebSocketContainer` bean, which encapsulates the low-level connection and `WebSocketSession` handling operations.<br/>Required.                                                                                                                                           |
|**4**|                                                                                  Optional reference to a `SubProtocolHandler` instance.<br/>It is used when the client did not request a sub-protocol or it is a single protocol-handler.<br/>If this reference or a `protocol-handlers` list is not provided, the `PassThruSubProtocolHandler` is used by default.                                                                                  |
|**5**|                                     List of `SubProtocolHandler` bean references for this channel adapter.<br/>If you provide only a single bean reference and do not provide a `default-protocol-handler`, that single `SubProtocolHandler` is used as the `default-protocol-handler`.<br/>If you do not set this attribute or `default-protocol-handler`, the `PassThruSubProtocolHandler` is used by default.                                     |
|**6**|                                                                                                                                                                                         List of `MessageConverter` bean references for this channel adapter.                                                                                                                                                                                         |
|**7**|Boolean value indicating whether the default converters should be registered after any custom converters.<br/>This flag is used only if `message-converters` is provided.<br/>Otherwise, all default converters are registered.<br/>Defaults to `false`.<br/>The default converters are (in order): `StringMessageConverter`, `ByteArrayMessageConverter`, and `MappingJackson2MessageConverter` (if the Jackson library is present on the classpath).|
|**8**|                                                                                                                                                                          Boolean value indicating whether this endpoint should start automatically.<br/>Defaults to `true`.                                                                                                                                                                          |
|**9**|                                           The lifecycle phase within which this endpoint should start and stop.<br/>The lower the value, the earlier this endpoint starts and the later it stops.<br/>The default is `Integer.MIN_VALUE`.<br/>Values can be negative.<br/>See [`SmartLifeCycle`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/SmartLifecycle.html).                                            |

#### `<int-websocket:inbound-channel-adapter>` Attributes

The following listing shows the attributes available for the `<int-websocket:outbound-channel-adapter>` element:

```
<int-websocket:inbound-channel-adapter
                            id=""  (1)
                            channel=""  (2)
                            error-channel=""  (3)
                            container=""  (4)
                            default-protocol-handler=""  (5)
                            protocol-handlers=""  (6)
                            message-converters=""  (7)
                            merge-with-default-converters=""  (8)
                            send-timeout=""  (9)
                            payload-type=""  (10)
                            use-broker=""  (11)
                            auto-startup=""  (12)
                            phase=""/>  (13)
```

|**1** |                                                       The component bean name.<br/>If you do not set the `channel` attribute, a `DirectChannel` is created and registered in the application context with this `id` attribute as the bean name.<br/>In this case, the endpoint is registered with the bean name `id` plus `.adapter`.                                                        |
|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|**2** |                                                                                                                                                                       Identifies the channel attached to this adapter.                                                                                                                                                                       |
|**3** |                                                                                                                                                  The `MessageChannel` bean reference to which the `ErrorMessage` instances should be sent.                                                                                                                                                   |
|**4** |                                                                                                                                   See the same option on the [`<int-websocket:outbound-channel-adapter>`](#websocket-outbound-channel-adapter-attributes).                                                                                                                                   |
|**5** |                                                                                                                                   See the same option on the [`<int-websocket:outbound-channel-adapter>`](#websocket-outbound-channel-adapter-attributes).                                                                                                                                   |
|**6** |                                                                                                                                   See the same option on the [`<int-websocket:outbound-channel-adapter>`](#websocket-outbound-channel-adapter-attributes).                                                                                                                                   |
|**7** |                                                                                                                                   See the same option on the [`<int-websocket:outbound-channel-adapter>`](#websocket-outbound-channel-adapter-attributes).                                                                                                                                   |
|**8** |                                                                                                                                   See the same option on the [`<int-websocket:outbound-channel-adapter>`](#websocket-outbound-channel-adapter-attributes).                                                                                                                                   |
|**9** |                                                                               Maximum amount of time (in milliseconds) to wait when sending a message to the channel if the channel can block.<br/>For example, a `QueueChannel` can block until space is available if its maximum capacity has been reached.                                                                                |
|**10**|                                                                                                                     Fully qualified name of the Java type for the target `payload` to convert from the incoming `WebSocketMessage`.<br/>Defaults to `java.lang.String`.                                                                                                                      |
|**11**|Indicates whether this adapter sends `non-MESSAGE` `WebSocketMessage` instances and messages with broker destinations to the `AbstractBrokerMessageHandler` from the application context.<br/>When this attribute is `true`, the `Broker Relay` configuration is required.<br/>This attribute is used only on the server side.<br/>On the client side, it is ignored.<br/>Defaults to `false`.|
|**12**|                                                                                                                                   See the same option on the [`<int-websocket:outbound-channel-adapter>`](#websocket-outbound-channel-adapter-attributes).                                                                                                                                   |
|**13**|                                                                                                                                   See the same option on the [`<int-websocket:outbound-channel-adapter>`](#websocket-outbound-channel-adapter-attributes).                                                                                                                                   |

### Using `ClientStompEncoder`

Starting with version 4.3.13, Spring Integration provides `ClientStompEncoder` (as an extension of the standard `StompEncoder`) for use on the client side of WebSocket channel adapters.
For proper client side message preparation, you must inject an instance of the `ClientStompEncoder` into the `StompSubProtocolHandler`.
One problem with the default `StompSubProtocolHandler` is that it was designed for the server side, so it updates the `SEND` `stompCommand` header into `MESSAGE` (as required by the STOMP protocol for the server side).
If the client does not send its messages in the proper `SEND` web socket frame, some STOMP brokers do not accept them.
The purpose of the `ClientStompEncoder`, in this case, is to override the `stompCommand` header and set it to the `SEND` value before encoding the message to the `byte[]`.

### Dynamic WebSocket Endpoints Registration

Starting with version 5.5, the WebSocket server endpoints (channel adapters based on a `ServerWebSocketContainer`) can now be registered (and removed) at runtime - the `paths` a `ServerWebSocketContainer` is mapped is exposed via `HandlerMapping` into a `DispatcherServlet` and accessible for WebSocket clients.
The [Dynamic and Runtime Integration Flows](./dsl.html#java-dsl-runtime-flows) support helps to register these endpoints in a transparent manner:

```
@Autowired
IntegrationFlowContext integrationFlowContext;

@Autowired
HandshakeHandler handshakeHandler;
...
ServerWebSocketContainer serverWebSocketContainer =
       new ServerWebSocketContainer("/dynamic")
               .setHandshakeHandler(this.handshakeHandler);

WebSocketInboundChannelAdapter webSocketInboundChannelAdapter =
       new WebSocketInboundChannelAdapter(serverWebSocketContainer);

QueueChannel dynamicRequestsChannel = new QueueChannel();

IntegrationFlow serverFlow =
       IntegrationFlows.from(webSocketInboundChannelAdapter)
               .channel(dynamicRequestsChannel)
               .get();

IntegrationFlowContext.IntegrationFlowRegistration dynamicServerFlow =
       this.integrationFlowContext.registration(serverFlow)
               .addBean(serverWebSocketContainer)
               .register();
...
dynamicServerFlow.destroy();
```

|   |It is important to call `.addBean(serverWebSocketContainer)` on the dynamic flow registration to add the instance of `ServerWebSocketContainer` into an `ApplicationContext` for endpoint registration.<br/>When a dynamic flow registration is destroyed, the associated `ServerWebSocketContainer` instance is destroyed, too, as well as the respective endpoint registration, including URL path mappings.|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

|   |The dynamic Websocket endpoints can only be registered via Spring Integration mechanism: when regular Spring `@EnableWebsocket` is used, Spring Integration configuration backs off and no infrastructure for dynamic endpoints is registered.|
|---|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|