Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
React native: event source is not closing after logout

I want to close event source after user logout. My codes are below. When user logging out push notifications are still coming. My questions is how to close event source?

 const eventSource = new EventSource(`${environment.notificationUrl}/subscribe? 
  token=${token}`);

  if (!token) {
  eventSource.removeAllEventListeners()
  eventSource.close()
  return;
 }


 eventSource.addEventListener('message', (event: any) => {
 ---- some codes here ----
 }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Technically, event listeners should be subscribed inside useEffect and keep tracking when user logged out.

const NotificationSubscriptionManager = () => {
  const eventSource =
    new EventSource(`${environment.notificationUrl}/subscribe? 
  token=${token}`);

  const subScribeToNotifications = () => {
    eventSource.addEventListener("message", (event: any) => {
      /// ---- some codes here ----
    });
  };

  const unsubScribeToNotifications = () => {
    eventSource.removeAllEventListeners();
    eventSource.close();
  };

  useEffect(() => {
    if (!token) {
      return unsubScribeToNotifications();
    }

    // Subscribe to notification events
    subScribeToNotifications();

    // Cancel all subscription when component unmount to avoid
    // memory leak

    return () => {
      unsubScribeToNotifications();
    };
  }, [token]);
};



about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!