Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda