Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda