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.

Methods

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

https://gist.github.com/00fb8e9bf904a7e7033a0ae7da3341ea

Service Controller – EmpApiDemoController.js

https://gist.github.com/646b60010e5a7432cb824619a58bd5bc

Demo Component – AccountSpy.cmp

https://gist.github.com/a70d7b77f7e763b4073a64e761da316d

Demo Controller – AccountSpyController.js

https://gist.github.com/b5bf6ba4501aaeb95814a748fd262e19

Custom Component Event – EmpEvent.evt

https://gist.github.com/8e734e2f6c3ab0b7ff8c96b21e979486

PushTopic Creation

https://gist.github.com/6b3cab4f0e117a1de62c07a232c02fd9

Sample Payload

https://gist.github.com/5b6715780f1d9016fe9f319a29814d70

Output

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

SFDCFacts

  • 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 Salesforce.com Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has 11 Comments

  1. Avatar

    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.

    Thanks
    Raghu

    1. Avatar

      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. Avatar

    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. Avatar

      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. Avatar

    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. Avatar

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

  4. Avatar

    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 https://ibirdstechshare.blogspot.com/2018/10/lightningempapi-salesforce-winter19.html. Do mobile devices supported for ampApi or not?

  5. Avatar

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

  6. Avatar

    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.

Leave a Reply

Close Menu