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

169
Views
Receive data continuously from socket react

I'm currently using this code for receiving data from sockets Code:

 socket.on('send_image', (message) => {
        setImage(message.image)
        console.log(message)
      })

the server sends data(images from camera) continuously. how to receive it without refreshing? i tried one method that requests data every milliseconds. i don't know whether it is a good way or not

Request every milliseconds code:

 useEffect(() => {
    const id = setInterval(() => {

      socket.on('send_image', (message) => {
        setImage(message.image)
        console.log(message)
      })
      
    }, 10);
    return () => clearInterval(id);
  }, []); 

is there any other way to continuously receive data from sockets?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To update the local state as soon as a send_image message is received, just register the handler in your useEffect.

useEffect(() => {
  const handler = (message) => {
    setImage(message.image);
    console.log(message);
  };
  socket.on("send_image", handler);
  return () => socket.off("send_image", handler); // assuming `.off` deregisters a callback
}, []);
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!