This repository contains my spring-boot test application which is essentially a combination of two tutorials:
- Spring Integration MQTT
- Spring Boot Websockets You can find a live version of this at 123k.de
#Messaging Configuration
We have a @Configuration in MessageConfig. There we use the MQTT receiver from the tutorial.
The MQTT receiver is a ChannelAdapter from a spring-integration point of view. This initially feeds the MQTT messages
to the messaging-system (a DirectChannel called mqttInputChannel).
Then we have a @Router which listens on mqttInputChannel and
routes messages based on payload either to officeStream or OutsideStream. Additionally
all messages are published on the temperatureStream. All of those channels are PublishSubscribeChannel so that
we can install more listeners later.
#Websocket Configuration
On the other hand we have the websocket-configuration in WebsocketConfig.
This is all just a copy of the websocket tutorial. We initialise a simple broker which holds topics at /topic and provides a stomp-endpoint on temperature.
On client side we create a SocksJS socket and boot a StompClient.
There we register to two topics
socket = new SockJS('/temperature')
@stompClient = Stomp.over(socket)
@obelometer = new Obelometer()
connect = () =>
showOfficeTemperature = (message) =>
@obelometer.showTemperature(JSON.parse(message.body))
showOutsideTemperature = (message) =>
@showOutsideTemperature(JSON.parse(message.body))
@stompClient.subscribe '/topic/officeStream', showOfficeTemperature
@stompClient.subscribe '/topic/outsideStream', showOutsideTemperature
@stompClient.connect({}, connect)#Glue Code So now we have the spring-boot integration messaging infrastructure and the websocket simple broker. What we have to do is to route the messages to the broker handling websockets.
The following lines are the glue code.
private final SimpMessagingTemplate template;
@ServiceActivator(inputChannel = "officeStream")
public void officeMessage(Message<TemperatureMessage> message) throws Exception {
this.template.convertAndSend("/topic/officeStream", message.getPayload());
}This listens on the input-channel where we routed our MQTT messages to and sends those messages to the websocket-topic via the messaging-template utilizing the simple-broker which handles all the websocket-integration.
#Event Source If you really want to try this application and you want to have a simple event-source you can checkout the cimon application.
#Open Ends This naïve approach just worked. With the abstraction of spring-integration one can easily route any incoming message to websocket listeners. What currently is missing:
- Push the last seen temperatures
onConnectto the new WebSocket client - Store and aggregate the temperatures to provide a history view