Calculate if distance is in radius approximate in realtime

Viewed 16

im struggling with an advanced problem for a while. So decide get help the achieve this goal.

In my firebase realtime database is store my Users. The User location is always up to date when they are online. Each User looks like this:

userId: {
 isOnline: boolean;
 radius: number;
 location: {
  lat: number,
  lng: number
 }
 friends: string[],
 friendsInRadius: string[]
}

I want to achieve that every user

isOnline: true

iterate over his friends. Collect every friend

isOnline: true

and calculate the distance between them.

Then push the friend into friendsInRadius when the distance is smaller then the smallest radius of them.

Example 1: (isInRadius not fulfilled)

User_A: {
 radius: 500m
}
User_B: {
 radius: 100m
}
distanceBetween_User_A_User_B = 400m;
smallestRadius: 100m;
isInRadius = distanceBetween_User_A_User_B (400m) < smallestRadius (100m); (false)

So since isInRadius is false, dont push them into friendsInRadius.

Example 2 (isInRadius fulfilled):

User_A: {
 radius: 300m
}
User_B: {
 radius: 500m
}
distanceBetween_User_A_User_B = 200m;
smallestRadius: 300m;
isRadius = distanceBetween_User_A_User_B (200m) < smallestRadius (300m); (true)

Therefore isInRadius is true, push them into friendsInRadius.

I want approximate realtime experience like calculation on the server every 5 seconds.

The Calculation needs to be on the Server. My actual solution is that the user every 5 second call a firebase cloud function, but this is a way too expensive. By 12 request every minute, 720 every hour and so on for every online user.

So my question is there a cheaper way then use firebase clouds functions? I think about to use maybe firebase geofire.

Thank you guys to invest some of your brains energy!

0 Answers
Related