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

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

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 Relatório

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 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