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

97
Visualizações
`await` blocks node.js ability to receive and process websocket communication

I have a node script that spawns a random number of websocket connection.

In that same script, I await a call to my server to ping the websockets to count the number of responses within a timeout.

The problem is that the await blocks the websocket's ability to respond and they only DO respond after the await is resolved (after some timeout).

How can I spawn websockets, await a request and NOT block the websockets in the same script?

const axios = require('axios');
const { io } = require('socket.io-client');

const sock = async ({_id}) => {

  let socket = await io("ws://localhost:54321")

  socket.on('connect',(args) => {
    console.log(`${_id} connected to ws.`);
  })

  socket.on('ping',args => {
    console.log("PING RECEIVED ("+_id+")")
    socket.emit('pong',{socketId:_id,...args})
  })

  return socket;
}



(async function go(){

  let g_sockets = [];
  await Promise.all(['so0','s01'].map(async _id => {
    let s = await sock({_id})
    g_sockets.push(s);
  }))

  await new Promise((res,rej) => {
    axios.get('http://localhost:54321/roomSize').then(r => {
      console.log(r.data);
      res(r.data)
    })
  })
})()

Here, the socket.on('ping',function) only fires AFTER the /roomSize HTTP request resolves, which means the /roomSize calculation is incorrect.

My Flask server route looks like:

ping_holder = {}

@app.route('/roomSize')
def getRoomSize():

    pingId = str(random.randint(0,1e6))
    ping_holder[ pingId ] = []
    data = { "pingId":pingId }
    socketio.emit('ping',data=data,broadcast=True)

    time.sleep(1)


    return jsonify(ping_holder[pingId])
about 4 years ago · Juan Pablo Isaza
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