Loop with socket function doesn't work - WebSocket

Viewed 19

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

1 Answers

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();

Related