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

138
Visualizações
socket.io on client side not listening to server events

The socket have to listen to the server when i send a new message.
The messages arrive because when i refresh the page i can see them.
Server side socket i know is working find because in other frontend app the same client socket is working find.
This socket is called every time the user select a new chat in the web.

Here is the socket service in my app:

import openSocket from "socket.io-client";

function connectToSocket() {
    return openSocket("http://localhost:8080");
}

export default connectToSocket;

And here is the code executed when user select a chat:

// how i am importing the socket
import openSocket from "../../Services/socket-io"

async function fetchMessages(ticketId) {
    try {
        const { data } = await api.get("/messages/" + ticketId, {
            params: { pageNumber },
        });

        if (ticketId === data.ticket.id) {
            await loadMessages(data, ticketId);
        }

        listenMessages(ticketId);

    } catch (err) {
        Toast.ToastError("Error trying to load messages");
    }
};

function listenMessages(ticketId) {
    const socket = openSocket();

    socket.on("connect", () => socket.emit("joinChatBox", ticketId));

    socket.on("appMessage", (data) => {
      if (data.action === "create") {       
        console.log(data);
      }

      if (data.action === "update") {
        console.log(data);
      }
    });
}

What i already tried:
Use the same socket version both in client and server (3.0.5).
Calling listenMessages every time a message is sent.
socket.io-client version 4.

about 4 years ago · Santiago Gelvez
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