Search code examples
androidsocket.iogeolocationreal-timeandroid-geofence

Handling and emitting of real time location updates to nearby users with scalability


I have seen similar questions from users wanting to accomplish something similar, but not to this detail and I hope it will help others who come down this road. Here is the scenario: I have an android application, and one of its features is to display (with permission) the users location (using a Google Map view) to other users on the application.

I have built a socket server from scratch using Socket.io. The socket server communicates with RethinkDB for real time updates. When the client emits a new location, the socket server updates their position on RethinkDB. The socket gets the updated data by subscribing to changes on the database and it is then emitted to other users on the socket for parsing/displaying on their maps.

The basic functionality is working just fine. Two users can see each other real time on the map. Now comes the issue I am facing. When you use the Uber application you only see vehicles within a certain vicinity of you. I would like to achieve something similar. My pain point is determining how best to only notify users who are within a given radius of each other. Some one in Florida does not need to see some one in California and I imagine this would be a massive strain if there were a lot of connections.

  1. My first thought: as each location update comes, emit to those that are nearby using a geospatial query. That seems like a lot of processing on the server if there are many connections and just not practical as it would emit to all users.

  2. My second (and currently pursued) thought is using Socket.io rooms to separate users based on their location somehow. In this case, I reverse geocode their location to get an address and put them in a room on the server for their State. This at least narrows down the amount of users that are sharing a room. This does work but it would only work in the U.S. and users that live on the border of one state may miss out on seeing a user in the next state over that is close by. Also, a state is still a large area. Users only need to see others within a 20 miles radius. Any further than that is not necessary. I think this has promise but it has some flaws.

  3. A third thought I am considering researching further is using Geofences of some sort. For example, sectioning a state into multiple overlapping Geofences. Each Geofence would have its own room on the socket. I believe this would be a large undertaking mapping each fence and I would have to decide if the client would know what geofence it is in, or if the server handles that logic. On top of that, unless each fence overlaps its neighboring fences edge, you wont see users in the next fence over that are close by.

So my question: Before I get too deep into one route I wanted to see if there was a better option or if I am over complicating this. I have done some digging to see if there was information on how Uber determines which drivers are close to the user without luck (probably geospatial query?). Their solution may not work in this situation anyway, as the users location may be changing as well while still receiving updates of nearby users, but its somewhere to start.


Solution

    1. wont work because imagine someone is at the west side of state border and someone else is on the other side of the border - they are just 100m from each other but your app does not put them in one bucket. 3. is exactly the same problem. Only 1. will work and there is no other way around it.