Reputation: 4295
I'm trying to add in WebSockets support using STOMP to a Spring MVC application that is configured using XML. So far this has gone really well, and I've managed to get a WebSockets server listening, and stomp.js can connect to it and send messages and receive responses.
What I've not managed to get working yet is the support for the server to send arbitrary messages to the client that aren't responses to one received from the client. That means this is actually just a more complicated version of REST so far, which isn't too useful.
My XML config looks like this:
<?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:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">
<websocket:message-broker>
<websocket:stomp-endpoint path="/api/websocket/stomp" allowed-origins="*">
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic,/queue" />
<websocket:message-converters>
<bean class="org.springframework.messaging.converter.MappingJackson2MessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</websocket:message-converters>
</websocket:message-broker>
<bean class="uk.co.grahamcox.webapp.DebugController">
<constructor-arg name="clock" ref="clock" />
<constructor-arg name="template" ref="brokerMessagingTemplate" />
</bean>
</beans>
(DebugController is a class that has a single method to return the server time, working fine as both a REST and a WS handler)
And on startup I get:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'brokerMessagingTemplate' is defined
The frustrating thing is that IntelliJ auto-completed the "brokerMessagingTemplate" reference for me, and I can click through on it to an @Bean definition in AbstractMessageBrokerConfiguration.
I'm assuming that I'm missing some bit of config in the XML to make this work, but I can't find out in the docs what this would be.
Any suggestions?
Upvotes: 3
Views: 3504
Reputation: 59086
support for the server to send arbitrary messages to the client that aren't responses to one received from the client.
One way to send messages to clients is for them to first subscribe to a topic — make sure to understand the difference between "application destination prefixes" and "broker prefixes". In this particular case, you want clients to subscribe to a broker destination and then your server can send messages to all those clients, at any time.
The best way to understand this is to check out the flow of messages in the reference documentation.
To send those messages, your application code needs a messaging template.
You can fix your code example by switching form bean name to the actual bean type SimpMessagingTemplate
.
<bean class="uk.co.grahamcox.webapp.DebugController">
<constructor-arg name="clock" ref="clock" />
<constructor-arg name="template" class="org.springframework.messaging.simp.SimpMessagingTemplate" />
</bean>
The reference documentation mentions that bean name but it seems that it's not registered with this name when using the XML configuration. Feel free to create a JIRA issue to improve this.
Upvotes: 2