Java Spring: Real-time status update to the client over REST API

10,395

I found a solution myself and used Web Sockets for that.

I used this approach from the Spring documentation:

https://spring.io/guides/gs/messaging-stomp-websocket/

It could help on sending the messages for each processed line to the front end listener (after the web socket topic/connection is started) but I used a different approach for the data import, I used batch insert so that was unavailable for me, but web sockets are capable of doing that.

Share:
10,395
V. Samma
Author by

V. Samma

Updated on June 16, 2022

Comments

  • V. Samma
    V. Samma almost 2 years

    I am developing a web application in Java Spring where I want the user to be able to upload a CSV file from the front-end and then see the real-time progress of the importing process and after importing he should be able to search individual entries from the imported data.

    The importing process would consist of actually uploading the file (sending it via REST API POST request) and then reading it and saving its contents to a database so the user would be able to search from this data.

    How could I show the real-time progress of this process? I found a tutorial for jQuery, which shows the progress of amount of data uploaded/transferred, but as the most the work is done while processing the uploaded file, I thought I would like a solution where before the line processing I find out the amount of lines in the file and then the user could see a live message like:

    Lines processed: 1 out of 10000

    It could update/change incrementally, but as one line is processed pretty quickly, showing each number of lines processed is not that important.

    Either way, the question is, what's the easiest way to send these messages from Spring REST API to the client?

  • Farouk
    Farouk over 5 years
    can you share your approach ?
  • V. Samma
    V. Samma over 5 years
    @Farouk Hi, I literally used the same code that's available in that tutorial I shared. I did modify it somewhat but the base is the same. But if you really want to see how I did it, I guess you can see it from my github: github.com/vsamma/DataProcessor