I have an component using lightning empAPi. In controller, I am using subscribe method and didn't using unsubscribe method. Is that required to use unsubscribe method in controller. IF required please let me know how to use it.

I have did some piece of code for unsubscribe method, please have a look and let me know if you need more details.

   empApi.subscribe(channel, replayId, subscribeCallback).then(function(subscription){
        component.set('v.subscription', subscription)


  • What exactly is your question here? If you want to unsubscribe from the events, then you do need to call the empApi.unsubscribe() function. – Jayant Das Jun 7 '19 at 21:17
  • Is that required to unsubscribe the event , when using subscribe method. – Ajay Jun 7 '19 at 21:21

Based on your comments it seems you are asking if you are required to use use lightning:empApi's unsubscribe() function.

There is no hard and fast requirement to use lightning:empApi's unsubscribe() function, if you don't need to. It will solely depend on your use case here. If you are writing a component where Users can only subscribe to events and do not want to or allow users to unsubscribe from the events, you don't offer a way to unsubscribe.

You will need to review what you want to implement and take a decision based on that if you need to provide the capability to unsubscribe from the events.

  • The problem I am facing here, My component is working for few users and if we loggout and again loggin then its working, every confusing sometimes same user working and sometime not working – Ajay Jun 11 '19 at 18:44

Yes, you should unsubscribe when your component unloads. By not doing so, the handlers will not be removed and may therefore leak memory, and the server may deliver notifications unnecessarily, consuming the platform event limits.

  • Thanks for your response, My concern is functionalities are working but not at everytime. so you meant, bcz of unsubscribe method memory leaking. if we used unsubscribed method, will be alight ? – Ajay Jun 11 '19 at 18:28
  • Thanks for your response, My concern is functionalities are working but not at every time. so you meant, bcz of unsubscribe method memory is leaking. while using unsubscribed method, do we need to create handle action in component or please suggest me how to avoid memory leaking. so far i can see example like creating using separate action for unsubscribe method, but i don't need to create separate, while follwoin subscribe i need to unsubscribe it – Ajay Jun 11 '19 at 18:40
  • @Ajay for Aura components, you can use the unrender handler to unsubscribe, and for LWC components, disconnectedCallback. – sfdcfox Jun 11 '19 at 20:27
  • @sfdcfox disconnectedCallback does not seem to be reliably working. In our case, we have a Case record page with LWC inside and we use empAPI inside the component to receive filtered subscriptions for that case. As we move from one case record to the other, disconnectedCallback is not called at all. Strangely, it is getting invoked when I go the case which is already opened once (lets say i move from case number 1001 to 1002 and then back to 1001). How to reliably call disconnectedCallback as we move from one case to another. – Mohan P Feb 25 '20 at 19:48

If more than 2k subscribed for an event then this won't work. Make sure every time we subscribe, at the same time after completing the action we should unsubscribe otherwise limitation issue comes up

  • Is there an alternative approach you'd recommend or a way to limit the number of subscribers? – nbrown Nov 12 '20 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.