4 ms·
Can someone please give advice on how to accomplish the following with phoenix sockets? Say I have processes for each physical device in the world that are mov
by moomoo11 4y ago
Can someone please give advice on how to accomplish the following with phoenix sockets?
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..
- hdra 4y agowhy do you need the phoenix part? maybe consider a short-distance wireless comm like bluetooth or zigbee and have them broadcast to nearby devices? have the devices respond to the re-check broadcast and physics will take care of the locating nearby devices part
- conradfr 4y ago(not sure where the "phoenix sockets" fit into it) 1/ So I guess you're saying 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 (thanks to the Registry) to run the check. 6/ Done? (sure you can do a barebone version without the Registry or Presence but where's the fun in that?) [0] https://hexdocs.pm/elixir/1.14.3/Registry.html https://hexdocs.pm/elixir/1.14.3/Registry.html [1] https://hexdocs.pm/phoenix/Phoenix.Presence.html https://hexdocs.pm/phoenix/Phoenix.Presence.html [2] https://github.com/yltsrc/geocalc https://github.com/yltsrc/geocalc
- rkangel 4y agoSimplest thing is to use Phoenix PubSub. Broadcast an "anomaly at x,y,z coordinates" message. Then each process can calculate whether it is close and needs to do anything. This isn't very efficient because every process will do some work for every anomaly, but unless your system is very large that's probably OK (it as least extremely parallelised). 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.
- deleted 4y ago[deleted]
- grupthink 4y agoI stopped using Phoenix a few years ago, but I'd do something roughly as follows: - 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.
- peoplefromibiza 4y agosimple solution: store the geohashes and compare prefixes, each prefix length is equivalent to a distance. https://en.wikipedia.org/wiki/Geohash#Digits_and_precision_in_km https://en.wikipedia.org/wiki/Geohash#Digits_and_precision_i... you can use this pure Elixir library https://hex.pm/packages/geohash https://hex.pm/packages/geohash or this NIF that wraps a much faster C library https://hex.pm/packages/geohash_nif https://hex.pm/packages/geohash_nif or use one of the many geo libraries, like this one https://github.com/yltsrc/geocalc https://github.com/yltsrc/geocalc