MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.
https://geofirestore.com
MIT License
505 stars 58 forks source link

Returning a Collection of key, location and distance #5

Closed marcoredoxigen closed 6 years ago

marcoredoxigen commented 6 years ago

I want to wrap your library in a google cloud function and return an array with the results to the client.

I do the following setup.

    const geoQuery = geofire.query({
        center: [parseFloat(req.query.lat), parseFloat(req.query.lon)],
        radius: parseFloat(req.query.radius)
    })

Is there any way to get the result, instead of waiting for all onKeyEnteredRegistration Events? I need to do a response.send(results), but I Do not know which is the last item of the result set.

I had tried to put the response.send()inside the onReadyRegistration, but it happens sometimes before receiving all events in onKeyEnteredRegistration;

Any guidance appreciated.

MichaelSolati commented 6 years ago

Unfortunately the answer would be no. The library is based off of the official firebase geofire library and makes multiple queries around the area you selected as well as validates that it is really within the radius you provided. However the turn around time is fairly quick. What I would suggest is to wrap the response.send(result) in a setTimeout after maybe 4 seconds? (Maybe more??)

Then as the onKeyEnteredRegistration is fired push the results into the results array.

uturnr commented 6 years ago

Here's my ugly solution.

I create a promise with a timer that cancels the query and resolves in 10000ms. Every time key_entered is fired, I reset the timer to 500ms. When ready fires, I reset the timer to 1000ms.

It needs work, but it my most reliable solution so far, and my function currently returns data for a location with 21 documents in about 1600ms (3700ms on a cold start), and for a location with 0 documents in about 1100ms.*

return new Promise(resolve => {
    const geoQuery = geoFirestore.query({
      center: geoPoint,
      radius: 30
    });

    let geoQueryTimer = setTimeout(() => {
      geoQuery.cancel();
      resolve()
    }, 10000 );

    const onKeyEnteredRegistration = geoQuery.on(
      'key_entered',
      (key, doc, distance) => {
        clearTimeout(geoQueryTimer);
    // do stuff with doc
    // push to array, etc
        geoQueryTimer = setTimeout(() => {
          geoQuery.cancel();
          resolve()
        }, 500 );
      }
    );

    const onReadyRegistration = geoQuery.on(
      'ready',
      () => {
        clearTimeout(geoQueryTimer);
        geoQueryTimer = setTimeout(() => {
          geoQuery.cancel();
          resolve()
        }, 1000 );
      }
    )

  }).then(() => {
    // return data
  });

*edit: Times when testing functions locally in functions shell.

graig12 commented 6 years ago

What if you need to do another Firestore database call within the key_entred event? How do u suggest we do this.