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

213
Vistas
How to use React client to receive a message from a Socket.io server?

I'm using a server (Node.js and Socket.io) to send a message between clients (React). I can send a message from Client 1 to Client 2, but for the second client to be able to visualize the message on the screen it has to click a button. What I'm trying to achieve is to for the Client 2 to be able to visualize the message sent by the Client 1 automatically, without clicking on any button, directly when the Client 1 sends the message to the server and the server emits it. Something like having a listener that is working permanently looking for each time that the servers send a "socket.emit("send_message", message)". The problem is that on the React client, my function "socket.on("send_message",...)" inside a useEffect that only works when I click the button.

Any help or references would be awesome, thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I'm not sure if this the right approach but it worked for me.

Assuming the socket has been setup to connect to some url,

let [socket, setSocket] = useState(null)
useEffect(() => setSocket(io(socket_connect_url), [])

Then you could setup a hook to detect the event 'someEvent' like so,

  useEffect(() => {
    if(!socket) return;
    socket.on('someEvent', (recv) =>{
        try{
            // Process recv message here
            console.log(recv);
        }catch(err){
            console.log(err)
        }
    }, [socket])
  })
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