0

I'm trying to save a location of delivery guy in the database for each 1 second.

Using socket io, I listen to the client (delivery guy) and print his location and save it using mongoose.

It works when the socket listens to low number of clients. But I tried to listen to 5000 clients simultaneously, the saving operation is not getting executed.

Here is my code

  io.of('/orders').on('connection', (socket) => {
    socket.on('driver-location',  async ({ id, location }) => { // Listing to `driver-location` event.
      
    try {
        console.log(`${id} - driver's Location: ${location}`); // Console id and location, getting executed !

        var lat = parseFloat(location.split(',')[0]); // Get latitude
        var lng = parseFloat(location.split(',')[1]); // Get longitude 

        var newLocation = new Location({ // Create new location with attributes 
          name: 'Mock Name',
          order_id: '####',
          location: {
            type: 'Point',
            coordinates: [lng, lat]
          },
        });

        let saved = await newLocation.save(); // Not getting executed !
        
        return saved; // end of function

      } catch (error) {
        return console.log(error);
      }

    });
  });

I would really appreciate your help, if there is questions regarding my code please feel free to ask.

Faisal
  • 165
  • 1
  • 1
  • 7
  • try this solution if you have many concurrent connections? https://stackoverflow.com/questions/15872788/maximum-concurrent-socket-io-connections – Heartbit Jul 08 '21 at 08:44
  • @Saeid-a Thanks for the reference, I keep getting the locations from the clients concurrently, so it is not issue. But the main problem is with the operation call to database, it's not getting executed, I believe the issue is with mongoose I guess ? – Faisal Jul 08 '21 at 09:12
  • I think this should be due to `mongodb`. Do you have any `sharded cluster`? or do you have enough resources for your host server? – Heartbit Jul 08 '21 at 10:19
  • You should read about concurrency of mongodb first of any changes here => https://docs.mongodb.com/manual/faq/concurrency/#faq-concurrency – Heartbit Jul 08 '21 at 10:22
  • How did you determine it was not getting executed? – Joe Jul 10 '21 at 19:38
  • @Joe Because the data was not getting saved into database – Faisal Jul 11 '21 at 11:23
  • @Saeid-a Thanks Saeid for your help, really appreciate it ! – Faisal Jul 11 '21 at 11:24

0 Answers0