@@ -1823,6 +1823,106 @@ to access information about the message.
18231823---- 
18241824
18251825
1826+ [[websocket-stomp-client]]
1827+ === STOMP Client
1828+ 
1829+ Spring provides STOMP/WebSocket client and STOMP/TCP client support with the
1830+ following built-in choices (other libraries can be adapted):
1831+ 
1832+ * `WebSocketStompClient` built on the Spring WebSocket API with
1833+   support for standard JSR-356 WebSocket, Jetty 9, as well as SockJS
1834+   for HTTP-based WebSocket emulation (see <<websocket-fallback-sockjs-client>>).
1835+ * `Reactor11TcpStompClient` built on `NettyTcpClient` from the reactor-net project.
1836+ 
1837+ To begin, create and configure the client:
1838+ 
1839+ [source,java,indent=0]
1840+ [subs="verbatim,quotes"]
1841+ ---- 
1842+ WebSocketClient transport = new StandardWebSocketClient(); 
1843+ WebSocketStompClient stompClient = new WebSocketStompClient(transport); 
1844+ stompClient.setMessageConverter(new StringMessageConverter()); 
1845+ stompClient.setTaskScheduler(taskScheduler); // for heartbeats, receipts 
1846+ ---- 
1847+ 
1848+ Then connect to the WebSocket and provide a handler for the STOMP session:
1849+ 
1850+ [source,java,indent=0]
1851+ [subs="verbatim,quotes"]
1852+ ---- 
1853+ String url = "ws://127.0.0.1:8080/endpoint"; 
1854+ StompSessionHandler handler = ... ; 
1855+ stompClient.connect(url, handler); 
1856+ ---- 
1857+ 
1858+ When the session is ready for use, the handler is notified:
1859+ 
1860+ [source,java,indent=0]
1861+ [subs="verbatim,quotes"]
1862+ ---- 
1863+ public class MySessionHandler extends StompSessionHandlerAdapter { 
1864+ 
1865+     @Override 
1866+     public void afterConnected(StompSession session, StompHeaders connectedHeaders) { 
1867+         // ... 
1868+     } 
1869+ } 
1870+ ---- 
1871+ 
1872+ Send any Object as the payload and it will be serialized with a `MessageConverter`:
1873+  
1874+ [source,java,indent=0]
1875+ [subs="verbatim,quotes"]
1876+ ---- 
1877+ session.send("/topic/foo", "payload"); 
1878+ ---- 
1879+ 
1880+ The subscribe methods take a `StompFrameHandler` for messages on the subscription
1881+ and return a `Subscription` handle for unsubscribing. For each received message,
1882+ the handler must help to select the target type and then handle the
1883+ deserialized payload:
1884+  
1885+ [source,java,indent=0]
1886+ [subs="verbatim,quotes"]
1887+ ---- 
1888+ session.subscribe("/topic/foo", new StompFrameHandler() { 
1889+ 
1890+     @Override 
1891+     public Type getPayloadType(StompHeaders headers) { 
1892+         return String.class; 
1893+     } 
1894+ 
1895+     @Override 
1896+     public void handleFrame(StompHeaders headers, Object payload) { 
1897+         // ... 
1898+     } 
1899+ 
1900+ }); 
1901+ ---- 
1902+ 
1903+ STOMP supports heartbeats. To use this feature simply configure the
1904+ `WebSocketStompClient` with a `TaskScheduler` and if desired customize the
1905+ default heartbeat intervals (10, 10 seconds respectively by default) for
1906+ write inactivity which causes a heartbeat to be sent and for read
1907+ inactivity which closes the connection.
1908+ 
1909+ The STOMP protocol also supports receipts where the client must add a "receipt"
1910+ header to which the server responds with a RECEIPT frame after the send or
1911+ subscribe are processed. To support this the `StompSession` offers
1912+ `setAutoReceipt(boolean)` that causes a "receipt" header to be
1913+ added on every subsequent send or subscribe.
1914+ Alternatively you can also manually add a "receipt" header to the `StompHeaders`.
1915+ Both send and subscribe return an instance of `Receiptable`
1916+ that can be used to register for receipt success and failure callbacks.
1917+ For this feature the client must be configured with a `TaskScheduler`
1918+ and the amount of time before a receipt expires (15 seconds by default).
1919+ 
1920+ Note that `StompSessionHandler` itself is a `StompFrameHandler` which allows
1921+ it to handle ERROR frames in addition to the `handleException` callback for
1922+ exceptions from the handling of messages, and `handleTransportError` for
1923+ transport-level errors including `ConnectionLostException`.
1924+ 
1925+ 
18261926
18271927[[websocket-stomp-websocket-scope]]
18281928=== WebSocket Scope
@@ -2155,4 +2255,4 @@ to run a WebSocket server in embedded mode and connect to it as a WebSocket clie
21552255sending WebSocket messages containing STOMP frames.
21562256The https://github.com/rstoyanchev/spring-websocket-portfolio/tree/master/src/test/java/org/springframework/samples/portfolio/web[tests for the stock portfolio]
21572257sample application also demonstrates this approach using Tomcat as the embedded
2158- WebSocket server and a simple STOMP client for test purposes.
2258+ WebSocket server and a simple STOMP client for test purposes.
0 commit comments