1

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)
    })

     empApi.unsubscribe();

    },
  • 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 at 21:17
  • Is that required to unsubscribe the event , when using subscribe method. – Ajay Jun 7 at 21:21
1

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 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 at 18:40
  • @Ajay for Aura components, you can use the unrender handler to unsubscribe, and for LWC components, disconnectedCallback. – sfdcfox Jun 11 at 20:27
0

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 at 18:44

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.