I'm developing a web application using Spring Boot and React, that needs to return information about hotels from different cities:
- As an input of the controller, I receive a list of cities that can go up to 50. Additionally, the user can filter the search using many criteria (hotel rate, price, etc.)
- As an output, I have to return, for each city, the average cost of the hotels in the town plus a few other pieces of information.
I will be using third-party API to fetch hotel data (like Booking API, for example).
The problem is that fetching hotel data for a single city can take some time (up to 5 seconds). So bringing data for 50 cities can be very long! I could parallelize API calls, but depending on the number of users using the application, it would still take too much time to display the 50 hotel results.
I don't want a user to wait too long before displaying the hotels' data. As soon as information is retrieved from a single hotel, it's returned to the front-end. This way, UI, hotel data are continuously displayed until all are processed.
The same behavior you can find when you're searching for flights and flights are consistently displayed.
I was thinking about the following solutions:
- Use Server-Sent Events on the back-end. The problem is I do not know how to tell the front about all data returned, and complete the process is complete.
- Use a "classic" REST endpoint, and front-end to fetch parameter continuously until it receives all the data. On the first call, frontend will be collecting data, for example, of 5 hotels, then 7, etc.. I will be using different HTTP status codes to notify that all data is processed or not.
Could you tell me what would be the best way to do what I want? The server-sent events look good to me but seem to be more suitable in case of notifications.
The problem of that solution is I do not know how to tell the front that ALL data are returned and the process is complete.
But you know when 1 hotel is being calculated and when the calcs for the hotel ends. Could not you send 1 event at the beginning and 1 at the end for each hotel? You could also inform the summary (1/50, 2/50) in each starting event. It's eventual consistency, at some point in the future, the client will get all the responses. It doesn't need to know when.