2

I want to develop an application where I want to push the messages (or data) to UI from backend Spring boot application.

I have the following requirement -

  1. Consider there is a REST service that accepts the data from other applications using the POST method.
  2. This data will be pushed to UI.

OR

Consider that there is a background process running which generate events and we want to push these events to UI.

For this, I came across about the WebSocket component that we can use in the Spring Boot application.

However, is there any other settings required to make it possible to push the incoming data to the UI?

Any help is appreciated.

Thanks,

Avinash Deshmukh

1
  • You will need some kind of message broker, anyway. Either it`s Spring STOMP or other WS extensions (ActiveMQ over amqp, Azure Service Bus, etc.). Commented Jul 11, 2019 at 11:35

1 Answer 1

6

The backend cannot magically push updates to a client UI. The backend will have no way of knowing where the UI exists (i.e. what the UI's ip address is) and even if it did, it may not have access to establish a connection (due to firewalls or a NAT).

For this reason a client UI has to request updates. One way this could be done would be to have a timer in the UI application that polls for updates via REST. But this is essentially what websockets do - with much less overhead.

This is how common applications that you use everyday work all the time. So I'm not sure why you do not want to go down the websockets route.

...

Starting with Spring 5.0.5.RELEASE, it isn’t necessary to do any customization because of the improvement of @SendToUser annotation, that allows us to send a message to a user destination via “/user/{sessionId}/…” rather than “/user/{user}/…“.

That means the annotation works relying on the session id of the input message, effectively sending a reply to destination private to the session:

...

There is a good example over here:

https://www.baeldung.com/spring-websockets-sendtouser

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Rawb, thanks for your reply. I will go through the link provided by you and will try to understand it. Thanks again!!
Hi Rawb, I gone through the example that you have mentioned in the link. However, is it possible for you provide any example that can work on this use-case? About "So I'm not sure why you do not want to go down the websockets route. " -> I am trying this as there are some use cases where I need to push the events to UI. I came across about SseEmitter class
I dont know what your use case is but server sent events might work. They are very similar to websockets but I don't like them because they require the http connection to remain open. This means its not really feasible at a very large scale (lots of users or long connections) but they might be appropriate depending on what you need to do. They also mean that they will require your front end to know when the connection is dropped and reconnect. A connection could be dropped because of connectivity issues or a network change.
Hi Rawb, I am able to use the websocket with the help of SimpleMessagingTemplate class. With the help of this class, we can pass the messages to the topic or queue where the client can subscribe for the messages those are uploaded by the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.