Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
Loop with socket function doesn't work - WebSocket

I have a socket endpoint, that I connect to and send a message to get a user. I used this code to do it :

import generateConnection from './generate-connection';

export async function fetchUser(id: number) {
  return new Promise(function (resolve) {
    const connection = generateConnection();
    connection.onopen = () => {
      connection.send(
        JSON.stringify(
          '{"msg":"connect","version":"1","support":["1","pre2","pre1"]}',
        ),
      );
      connection.send(
        JSON.stringify(
          `{"msg":"method","id":"1","method":"Users.getUser","params":[${id}]}`,
        ),
      );
      console.log('Connected');
    };
    connection.on('message', async (event) => {
      const data = event.toString();
      if (data[0] == 'a') {
        const a = JSON.parse(JSON.parse(data.substring(1))[0]);
        if (a.msg == 'result') {
          if ('error' in a) {
            console.log('Error' + a.error.msg);
            return null;
          } else {
            resolve(a.result);
          }
        }
      }
    });
    connection.on('error', function (error) {
      console.log('Connection Error: ' + error.toString());
    });
    connection.on('close', function () {
      console.log('echo-protocol Connection Closed');
    });
  });
}

const fetchAllUsers = async () => {
  for (let i = 0; i < 100; i++) {
    const user: any = await fetchUser(i);
    console.log(user.name);
  }
};
fetchAllUsers();

I get the following result :

Connected
Jack

It just give me the first user and it stop on the second.

I have no control over the socket and I want to be able to fetch all 5000 users each day to be synced.

I'm Using WebSocket for this problem.

If you have any proposition other than this method, I'm all ears :D

To explain more :

1 - I want to open a connection

2 - Send a message

3 - get Result

4 - Add to Array or db

5 - When finished, close the connection.

6 - repeat

almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Why would you close the connection every time? An open connection allows you to send many messages. But maybe it is easier to:

connection.close()

// right before:
resolve(a.result);

If that didn't work maybe it's time to send more then one request per connection. Try this (I'm a little rusty with promises so I hope you get the idea and improve it)

import generateConnection from './generate-connection';

export async function fetchAllUsers() {
  var total = 100;
  var returned = 0;
  var all_results = [];

  return new Promise(function(resolve) {
    const connection = generateConnection();
    connection.onopen = () => {
      connection.send(
        JSON.stringify(
          '{"msg":"connect","version":"1","support":["1","pre2","pre1"]}',
        ),
      );

      for (var i = 0; i < total; i++) {
        connection.send(
          JSON.stringify(
            `{"msg":"method","id":"1","method":"Users.getUser","params":[${i}]}`,
          ),
        );
      }

      console.log('Connected');
    };
    connection.on('message', async(event) => {
      const data = event.toString();
      if (data[0] == 'a') {
        const a = JSON.parse(JSON.parse(data.substring(1))[0]);
        if (a.msg == 'result') {
          if ('error' in a) {
            console.log('Error' + a.error.msg);
            return null;
          } else {
            console.log(a.result.name);
            all_results.push(a.result);

            returned++;

            if (returned == total) {
              resolve(all_results);
            }

          }
        }
      }
    });
    connection.on('error', function(error) {
      console.log('Connection Error: ' + error.toString());
    });
    connection.on('close', function() {
      console.log('echo-protocol Connection Closed');
    });
  });
}


fetchAllUsers();

almost 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda