Search code examples
ruby-on-railsgeolocationruby-on-rails-5actioncable

Using ActionCable along with geolocation services


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.


Solution

  • 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.