Search code examples
javaspringbackendserver-sent-events

How create an endpoint for downloading a PDF and sharing the generation progress with Server-sent events


I have an endpoint which allow the user to download a PDF. As the PDF generation usually takes more than 20 seconds, I would like to send to the frontend information about the PDF generation progress while we are processing it. I had never used Server-sent events, but I did a simple test in Spring 4.3) and it works fine. But now I don't know how to use it for the PDF download.

Currently I'm writing the PDF in the response ("application/pdf"), but in this case I won't be able to return the object SseEmitter.

Do you have any suggestion for sending a progress stream to frontend and still being able to provide the PDF? Should I use websockets instead of SSE? Maybe two different endpoints? Any suggestion is welcome. :)


Solution

  • My solution was using two different endpoints:

    First endpoint: provide the progress stream and persist the PDF content in database Second endpoint: download the PDF

    I don't think I would be happy with this solution if I was not already persisting this PDF content in database anyway, but it was exactly the case, so it was fine. :)