lightning:empApi #Winter19 – Capture Streaming Events

lightning:empApi #Winter19 – Capture Streaming Events

Let’s explore one more powerful component of #Winter19. 

We are in the 4th Industrial Revolution which has a lot of scope for IoT(Internet of Things).
IoT loves event-driven architecture and so does Salesforce. Salesforce has introduced many different types of events in the past like Platform events, PushTopic events, Generic events, and Change Data Capture events. And with #Winter19, Salesforce has given us a powerful base lightning component lightning:empApi, which is capable of handling all these events (Yes, all of them) with minimal coding.


lightning:empApi has below methods for use:

  • subscribe – You need to call this method to subscribe to a channel to listen for events. It accepts channel name, replay id and callback method as arguments. This method will return subscription object.
  • unsubscribe – Call this method to unsubscribe from a channel. You need to pass subscription object and an optional callback method as arguments.
  • onError – This method will accept a callback method which will be called whenever an error is being thrown.
  • setDebugFlag – Call this method to turn on/off browser console logging.
  • isEmpEnabled – Returns true if the EmpJs Streaming API library can be used in this context; otherwise false. For example, if the component is hosted in Lightning Experience or Salesforce Mobile, this will return true. But if your component is hosted on communities/lightning out/visualforce/standalone app, then this will return false.

If you have gone through my previous post about service component to handle streaming event, we have created a similar component like lightning:empApi to handle the events. Let’s see the same “AccountSpy” example with lightning:empApi component.


Service Component – EmpApiDemo.cmp

Service Controller – EmpApiDemoController.js

Demo Component – AccountSpy.cmp

Demo Controller – AccountSpyController.js

Custom Component Event – EmpEvent.evt

PushTopic Creation

Sample Payload


Account Spy Component – Showing Account Record Changes
Browser Console Logs – Showing Account Record Changes


  • lightning:empApi component is capable of handling all type of events including recently introduced Change Data Capture events.
  • lightning:empApi is only supported in Lightning Experience and Salesforce Mobile App for now. This component will not work in Lightning Communities, Visualforce, Lightning Out, and Standalone Lightning App.
  • It’s always the best practice to unsubscribe from the event whenever a component is destroyed.
  • Use setDebugFlag method to turn on browser console debugging.

Reference: Lightning Component Library

This component is very powerful especially in IoT world as everything depends on events now. Please do share your feedback or queries in comments.

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has 12 Comments

  1. Hello Manish,

    Your blogs on Lightening developments are very useful. I have a used case: The case object status in Salesforce changes like 3 times in every 5 minutes. We want to update the same changes in real time on the Oracle database. Can this be accomplished using CDC events?
    Also, will there be a need for an tool like Mulesoft, Workato, Dell boomi etc, to perform these updates? Please advice. Thanks in advance.


    1. Yes, you can CDC events here. If you already have integration between oracle and salesforce, then you do not have to go for integration hub like mulesoft or other. If you do not have any integration service in place, then you can either create it using apex or can go for these integration tools.

  2. Hello Manish,
    I did all the steps you mentioned but subscription is not happening successfully. Any error is also not coming. What could be the reason?

    1. Have you created PushTopic? are you passing correct channel name? Can you debug your component? or check the browser console if any error appears there? Without knowing the error it would be hard to resolve it.

  3. Hi,

    I need to alert service agent if some other agent updates the viewing record. is there any way to achieve that on lightning?

    Thanks in advance

    1. You can use same change data capture events. Other ways would be to have platform events and implement in similar way

  4. Hi Manish,

    I have an issue on empApi with the Salesforce1 app. It is not working as expected. Whereas it is working fine on browsers. On the successful subscription, I put the alert message. I am getting that alert on browsers but not on Salesforce1 app. Also on account record update/ create i push the platform event from the trigger to have the live data in my component. I followed the same code from here Do mobile devices supported for ampApi or not?

  5. Hi Manish,
    Is there any alternative for this feature in the in Lightning Communities?

  6. Hi Manish,

    Is that required to use in controller unsubscribe method while using combination of subscribe method, bcz here i have an component using only one handler(doInit function) and in controller doInit func having subscribe method, So far my component is working like updating the event real time but very rare case its not updating then after loggout and loggin again working, i am not sure, what this was happening. please let me know if you any idea to resolve it.

  7. Hi Manish,
    Any workaround to get the platform events in Lightning Community page with lightning components?

Leave a Reply

Close Menu