J. Doe
J. Doe

Reputation: 101

How to notify the front-end of a website of the status of a back-end processing job?

I currently face the following issue:

After a user has uploaded his images, all images are processed through a script that optimizes every image (compresses it and removes EXIF-data).

I got everything working, the only problem is that the proces takes quite some time. I want to notify the user of the job status, e.g. a percentage of the processed images.

Currently, the user has to wait without knowing what's up in the back-end. What is the best way to accomplish this? I've thought about AJAX-calls, but I honestly have no idea where to start with implementing this, also because it looks like I need multiple calls (kinda like a heartbeat call on the processing job).

The application I am developing in is a Laravel application, I've made an API controller which handles incoming files via AJAX calls.

Any help is appreciated, thanks.

Upvotes: 1

Views: 2282

Answers (2)

Andy
Andy

Reputation: 5405

You'd be better off reading about the principle of how it's done, for example: Progress bar AJAX and PHP

Essentially the way it's done is that the job (processing images in your case) happens on the server through PHP. Your script will need to produce some sort of output to show how far through it is, e.g. echo some value for the percentage progress. The PHP script itself is responsible for producing this output, so you must work out how to calculate it and then code that in. It could be that it takes the number of images to be processed into account, and when each one is successfully processed, it adds 1 to a counter. When the counter equals the number of images, 100% done, or possibly some error messages if something went wrong.

On the frontend you could have an ajax script which reads the output from the PHP script. This in turn could update a progress bar, or div with some sort of percentage message - the value used coming from your PHP script.

Laravel - and other frameworks - have built-in methods to help. But you'd be better understanding the principles of how it works, such as on the link I posted.

Upvotes: 1

Jerodev
Jerodev

Reputation: 33196

Laravel has Broadcasting for this. It uses websockets, redis or pusher to send events to the client.

This way you can send the client a message when the processing is done without them having to refresh a webpage all the time.

Upvotes: 3

Related Questions