functions-samples icon indicating copy to clipboard operation
functions-samples copied to clipboard

On-demand Rideshare Service Example App for firebase cloud Functions (Uber/Lyft Clone)

Open Radecom opened this issue 6 years ago • 6 comments

I am designing a clone uber system with Firestore, Cloud Messaging and Functions of Firebase. I have a function that runs when a new order is created (OrdersRef.onCreate ()).

Diagram Rideshare Dispatch

Obviously, I execute the tasks of:

Get the currently available cars. For each available car, I get the distance between the location of the car and the pick-up point of the order. I order the cars by the distance of their location and the point of collection of the order. *** I have the exception if the array of available cars is empty, the function ends.*

So far everything is going well. But here you ask me, what is the best way to carry out the car dispatch?

I can think of 2 ways:

The first would be to send a notification to the nearest driver with the order data, wait 9 sec (sleeping the cloud function with a sleep function), after waiting 9 seconds, make a new query to get the current status of the order, and if it has not yet been taken, I send a new notification to the next driver ... and so on until I run out of my array of available cars. ** We have to remember that in cloud functions (backend) queries to Firestore are promises and not observable as in frontend frames (angular).

The second would be to schedule notifications and queries with pub/sub and make a review similarly ... Send a notification to the nearest driver with the order data, and program with Pub / Sub a function that will check in the next 9 seconds if the order has already been taken, if I do not send the notification to the next driver and reschedule a new function with Pub / Sub to recheck ... and so on until my array of available cars runs out.

The problem with the first form is that the function can last a long time in execution.

The problem with the second form is that a function can be converted into many subfunctions.

My question here is: What is the best way to do this?

Could you give an example to guide us like this Pubnun?

Radecom avatar Aug 19 '19 22:08 Radecom

Such an example would be great. Please do it!

IngAjVillalon avatar Aug 19 '19 22:08 IngAjVillalon

What an interesting example! I join the waiting list

Crewnie avatar Aug 22 '19 00:08 Crewnie

Wow, that would be great, Yes please and in more detail explain each element of google cloud we would have more idea of ​​how to start. Because sometimes that is the big problem, we don't even know how to start, and having a guide would be much easier.

FrutasLuna avatar Aug 22 '19 22:08 FrutasLuna

Cloud pubsub sounds like the better option here. Though if you want a simpler system, the first way is the way to go. Usually, you should go with the simpler way of implementation, then when your number of users get too much, that's when you implement a more advanced method. I've implemented something similar and I use arrays, plus computers are really fast, so it might not really take as long as you think it will, plus it's ok for clients to wait for some time. Hope this helps.

On Fri, Aug 23, 2019, 01:25 Frutas Luna [email protected] wrote:

Wow, that would be great, Yes please and in more detail explain each element of google cloud we would have more idea of ​​how to start. Because sometimes that is the big problem, we don't even know how to start, and having a guide would be much easier.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/firebase/functions-samples/issues/603?email_source=notifications&email_token=AJPSWCNGDYPVSSMWPWE2VP3QF4G4NA5CNFSM4INHUZYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD46SKKQ#issuecomment-524100906, or mute the thread https://github.com/notifications/unsubscribe-auth/AJPSWCL762SCAFBI2F6CP23QF4G4NANCNFSM4INHUZYA .

v1b3m avatar Aug 23 '19 04:08 v1b3m

Yes please, it would help us a lot, especially to have an overview of how to combine all the cloud functions of google cloud.

desarrollotrimax avatar Aug 29 '19 17:08 desarrollotrimax

@Radecom I also faced this issue and these were the steps I used in resolving it.

  1. Available driver coordinates are stored always stored in a collection and updated by the driver app.
  2. When a rider makes a ride request is made, in the onCreate, only Y number of available drivers within X km radius of the pick-up point based on distance from the pickup point will be sent push notifications. if no drivers are available, a notification is sent to the rider to try again in a few minutes.
  3. The first available driver to accept the request gets it and a notification is sent to the rider. The accept ride function does not allow drivers to accept a ride request after Z minutes from request time.
  4. The rider app quits the request after Z minutes if no driver has accepted it.

olaide-ekolere avatar Jan 05 '20 17:01 olaide-ekolere