- Imagine a grid where each cell is a 1x1 square mile based on lat/lon
- When a device moves to a new cell, have it join the phx-channel representing that 1x1 cell. It must also leave the previous phx-channel of the cell it occupied. If your devices move very slowly, have them recalculate the cell/channel they belong to once an hour (for example).
- When an anomaly occurs, send a message to the channel. Every device in that cell will receive the websocket message. You may also choose to send your message to the 3x3 cells that surround just the cell where the anomaly occurred.
- If you need to tell all devices something, use broadcast.
Feel free to change 1x1 mile to 1x1 meters or whatever.
https://en.wikipedia.org/wiki/Geohash#Digits_and_precision_i...
you can use this pure Elixir library
https://hex.pm/packages/geohash
or this NIF that wraps a much faster C library
https://hex.pm/packages/geohash_nif
or use one of the many geo libraries, like this one
1/ So I guess you say one process/GenServer per device, communicating with it.
2/ Use a Registry[0] to store the process names (based on the device id or whatever) to abstract them from their PID.
3/ Have another GenServer (let's call it CoordStore) storing a map of "process_name -> lat/long" (with the related processes sending updates of the coordinates to it)
4/ Use Presence[1] to keep track of the devices and remove them from the CoordStore when needed.
5/ When you have an anomaly on one device ask CoordStore for the nearby devices/processes (using maybe [2]) and ask each one to run the check.
6/ Done?
[0] https://hexdocs.pm/elixir/1.14.3/Registry.html
The alternate approach is to create some form of central process registry that has all the coordinates and process ids, so that you can look up in one place and then send messages out to the relevant processes. There are some other replies about this sort of approach.
Say I have processes for each physical device in the world that are moving around (like on water or something). Any time there is an “anomaly” I want to double check by having all nearby processes/devices run the same check again.
Is there a way to run a filter by geoquerying without having to rely on storing the coordinates in a separate store and querying for those device/process IDs? I can store the coordinates in each process, and I want to be able to find nearby processes in one go without using a separate system for finding them.
I’m quite a noob at phoenix..