I have recently developed a simple messaging application with Spring Boot and Spring Security. The application takes in 2 users - user A and user B. Once, user A performs a specific task a notification is sent to user B. Currently I am doing this by adding a Spring Messaging dependency and SockJS and it works great.
Here is where I am confused and hoping to receive some guidance. I realize there are many tutorials that speak about RabbitMQ and ActiveMQ. From what I understand, they are message brokers. May I ask what is the difference between SockJS and RabbitMQ/ActiveMQ? And do I need RabbitMQ/ActiveMQ in my current application together with SockJS?
SockJS is JavaScript based WebSocket client library that runs in a browser. It can be used to send messages to or receive messages from a broker.
Both RabbitMQ and ActiveMQ are message brokers, examples of message-oriented middleware. They both support WebSocket clients which use a messaging protocol (e.g. STOMP or AMQP). Brokers receive messages from and dispatch messages to clients.
You haven't really provided enough information to determine whether or not you actually need to use either RabbitMQ or ActiveMQ in your current application given that it's already working as it is.
Related
I have 2 spring boot applications
On-premise teller application
Cloud based multi-tenant application that aggregates data from all teller applications
The teller application has to work offline(if connectivity is down) as well. What is the best approach to broadcast events from the teller application to the cloud. I would not prefer to implement code to persist events.
What is the best approach? ReST/message broker or some other approach. If using a message broker, will the spring cloud stream abstraction queue events when the connection to the broker is down and retry.
I would go with the message broker (rabbit, kafka) and spring-cloud-stream, since your use case was exactly what/how it was designed.
The microservcice (your app) is a consumer of the broker, that is: it can publish to and/or consume events from the broker. If the app is down the broker is up and potentially collecting events destined to the down app. Once app is up it consumes queued up events and so on.
I'll stop here given the general nature of your question but feel free to follow up with more details.
I'm using websockets to build a chat server in springboot. Now my application lacks any kind of Spring Security information since it is intended to only act as a channel between two desktop clients.
So currently I'm able to broadcast messages to all clients, however, I would like to know, is it possible to create dynamic queues when a client connects to other user, (a queue is created and they both subscribe to same queue). The queue exists until one of the client disconnects.
I'm using SimpleMessageBroker priovided with Spring.
Can I send a message to a message broker using SimpMessagingTemplate#convertAndSendToUser or SimpMessagingTemplate#convertAndSend methods without settings up a websocket message broker using #EnableWebSocketMessageBroker?
What I'm trying to do is utilise one websocket server to provide messaging for two application server instances(One spring 4 and one Spring 3). I created a one web server with Spring 4, Spring boot plus websocket message broker enabled.
Now I want two application servers to push messages to rabbitmq so it will broadcast them to clients subscribed to it.
First issue I faced is if there is no websockt message broker configuration available, SimpMessagingTemplate will not get autowired to application context. I couldn't get it injected without creating a websocket message board either.
Please help me to find out whether this is possible.
BTW I have a previous question unanswered related to this.
Well, After reading lots of documentation I found the answer myself. The key thing is this architecture is following.
In this architecture spring act as a gateway for communication between the message broker and client. Spring doesn't do anything(Other than when it necessary) but forward the request to the message broker(STOMP messages). The configuration kept on Spring defines couple of important things. One is the exchange and other were routing keys. Spring configuration gives us an abstract layer so we subscribe and push messages to message broker without a fuss.
SimpMessagingTemplate is the abstract layer which we use to communicate with message broker. Spring creates the bean using the given details. Well I couldn't create a instance of SimpMessagingTemplate manually. I have to update Spring 3 application to Spring 4 in order to use websockets.
Since Spring and message broker is decoupled, clustering the application instance doesn't make any effect on message broker. Spring will communicate to message broker only when it need to subscribe to a channel or when it need to publish a message to a channel. So if there is two instances subscribing to same channel it would be two queues binding the one exchange using same routing key. Messages published into a channel will be available to all subscribers(queues) because they all use same routing key. Refer to rabbitmq stop plugin documentation for more elaborative description.
I need a simple web-app in spring-boot that listens for messages on a JMS queue and when arriving it should appear on a webpage via WebSocket.
I have searched for examples and found several individual; either WebSocket or JMS which I have tested on their own but have not succeeded in wiring it together.
I have searched for an example but not found any and in my mind I think it should be pretty easy since it's a very basic requirement.
Do you know about any example with JMS and HTML display via WebSocket that you can share or can give some hints or help for me to solve it?
The Spring Integration comes to the rescue.
You can write <int-jms:message-driven-channel-adapter> to read messages from JMS queue and forward them to the <int-websocket:outbound-channel-adapter>. Where the last one just sends messages to the connected WebSocket session(s).
See these Spring Integration samples on the matter:
https://github.com/spring-projects/spring-integration-samples/tree/master/basic/jms
https://github.com/spring-projects/spring-integration-samples/tree/master/basic/web-sockets
UPDATE
To send the message to all subscribed WebSocket session you should do something like this:
<int:splitter input-channel="enricheMessage" output-channel="sendMessage" apply-sequence="false">
<int-groovy:script>
#serverWebSocketContainer.sessions.keySet().collect {
org.springframework.integration.support.MessageBuilder.withPayload(payload)
.copyHeaders(headers)
.setHeader('simpSessionId', it)
.build()
}
</int-groovy:script>
</int:splitter>
With this Groovy script I retrieve session ids from the serverWebSocketContainer (all those connected clients), iterate over them to build messages to send them over their websocket. And split finally, to send to the <int-websocket:outbound-channel-adapter> one by one.
I have a tomcat7 application currently accepting https calls which carry a JSON payload. So this work perfectly in a client/server relationship.
I want to be able to push data out to 'clients' so am investigating using MQTT. This work fine - I can publish/subscribe messages between MQTT broker and the 'clients'.
I want now to be able to re-use my Tomcat code. Do I configure tomcat to publish/subscribe to MQTT topics? Do I make some 3rd process which subscribes to MQTT topic and calls into tomcat.
I'm at the beginning of my investigation stage of a project. Any help/recommendations are appreciated.
Yes it is possible, there are many client libraries available (e.g. Poho for Java), so your server can subscribe/publish messages. Now to handle multiple messages published from various clients, implement a message queue.
Checkout this, RabbitMQ MQTT Adapter
https://www.rabbitmq.com/mqtt.html