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

95
Views
`await` bloquea la capacidad de node.js para recibir y procesar la comunicación websocket

Tengo un script de node que genera un número aleatorio de conexiones websocket.

En ese mismo script, await una llamada a mi servidor para hacer ping a los websockets para contar la cantidad de respuestas dentro de un tiempo de espera.

El problema es que la await bloquea la capacidad de respuesta del websocket y solo SÍ responde después de que se resuelve la await (después de un tiempo de espera).

¿Cómo puedo generar websockets, esperar una solicitud y NO bloquear los websockets en el mismo 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) }) }) })()

Aquí, socket.on('ping',function) solo se activa DESPUÉS de que se resuelva la solicitud HTTP /roomSize , lo que significa que el cálculo /roomSize es incorrecto.

La ruta de mi servidor Flask se ve así:

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