0

I've got a fairly simple chat service up using Rails 5 and ActionCable. It allows the user to select 1 of a handful of rooms and it all works great and was super fun to setup.

The next issue I want to tackle is using geolocation to only display messages that are within X miles. I've got latitude and longitude in the Message model and I'm aware of geokit and geocoder so I know I can find messages within a certain distance to the user. What I'm having trouble conceptualizing is using this in conjunction with ActionCable. The idea sounded so easy in my head but when it came time to implement, I can't think of how to pull it off elegantly. Do I just allow all room messages through and filter them through JavaScript on the client? That seems kind of hacky and a waste of bandwidth for a bunch of messages being to the client without ever reaching the interface. Sadly, this is the only method I've been able to think of.

Steve E
  • 792
  • 10
  • 16
  • would you not perform a search for messages like so `@mesages = Message.where(message.distance_to(user.address) – Artem Ankudovich Jul 14 '16 at 14:28
  • This would work for getting the initial collection of recent messages but for the ActionCable broadcast, I would need to broadcast to only those users in range of that message. – Steve E Jul 14 '16 at 14:30
  • sorry no knowledge of ActionCable but the select there can be reversed where you can selectively choose which user gets what – Artem Ankudovich Jul 14 '16 at 14:32

1 Answers1

0

I managed to get this working by using maidenhead grid squares to divide the country up and broadcasting with ActionCable to the Maidenhead grid calculated from the latitude and longitude returned by the geolocation API. As the user, I subscribed him to listen to the maidenhead grid they were located and neighboring grids as far as I wanted the rough distance to be.

Steve E
  • 792
  • 10
  • 16