[SalesForce] Notify Lightning Component when ApexBatch finishes

What is the best way to notify the Lightning Component when ApexBatch (invoked by LC action method) finishes?

Use case – My LC is invoking ApexBatch to offload complex data processing. I want to let the user (still on the LC page), that the batch has completed, and they can now view the result.

I don't want to poll the server every X seconds to check batch job status, so looking for alternate approaches, which will minimize the # of API calls.

Best Answer

That's what the Enterprise Messaging Platform is for. You place a lightning:empApi in your component, and subscribe to listen to an event. Then, in your finish method, you use EventBus. You'll need to make sure that you have some sort of unique identifier (e.g. the Apex Batch job ID) so a client knows that their job is finished as opposed to any other job that might be running on the same channel. Make sure you unsubscribe when you're done listening.

Note: round trips to the server don't actually "cost" API calls. They're free to use as frequently as you'd like, including polling the server for updates. Granted, using EMP is still the preferred method, whenever possible, but API costs should not be a concern for normal applications and components.

Related Topic