1

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.

J. Doe
  • 101
  • 10
  • You can use `EventSource` to serve an event-stream to client, see [How to read and echo file size of uploaded file being written at server in real time without blocking at both server and client?](https://stackoverflow.com/q/42475492/) – guest271314 Jul 26 '17 at 14:34

2 Answers2

3

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.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
1

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.

Andy
  • 5,142
  • 11
  • 58
  • 131
  • That's not the point, this is a progress bar for the uploading of the images. But the images are already uploaded. – Jerodev Jul 26 '17 at 14:47
  • 1
    So what? The same principle applies. It doesn't matter what the job is. You have some intensive task to run on the server and want to display the progress on the client-side. It's better to understand how it works than just use something built in to a framework without understanding things properly. – Andy Jul 26 '17 at 14:48
  • 1
    That's not my point. You are correct, you should know how stuff works under the hood to get a better understanding. My point is that this is not an answer to the question of the OP. ;) – Jerodev Jul 26 '17 at 15:00