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

254
Views
Make JavaScript wait until websocket receives a reply

So as the title says, I'm playing around with websockets. So far all good, except for the fact that I can't make the page wait until we get a reponse. Now, I do actually have a good reason to do this; the requests times are about 2-10 ms due it going to 127.0.0.1 , so barely noticable. It is however long enough for the page to progress long enough to get to a part where the response is needed.
As you can tell, that is kind of a problem.
I am using the normal JS socket interface on the client and a golang websocket library on the backend.
Any ideas?

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

0

Just embed your part where a responds is needed inside

socket.onmessage = function (msg) {
    // part where msg.data is needed
}
about 4 years ago · Juan Pablo Isaza Report

0

Are you using some Web libraries like ReactJs/VueuJs?

If you are using some library that you can manage the Browser DOM, you can use a Spinner strategy. Wen, you page first rendering you send to the user at the first time a Spinner and before your data cames you remove the spinner and show your page. If you prefer you can use a Skeleton in place of the spinner.

If you are using react you can do something like this.

import { useState } from 'react'

const Page = () => {
 const [loading, setLoading] = useState(true)
 cosnt [data, setData] = useState({})

 useEffect(() => {
   socket.on('message', (data) => {
     setData(data)
     setLoading(false)
   })
 }, [])

 return (<>
    { loading ? <Spinner /> : <Screen data={data} /> }
 </>)
}
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!