3 min read
on May 7, 2014
How to build a proximity chat application with geohashing. This allows you to connect users in a chat room based on their geolocation. Includes full code, tutorial and demo.

Waving Hand

Good News! We’ve launched an all new Chat Resource Center.

We recommend checking out our new Chat Resource Center, which includes overviews, tutorials, and design patterns for building and deploying mobile and web chat.

Take me to the Chat Resource Center →

Chat App with Geohashing and Proximity

This geohashing tutorial walks you through how to build a real-time chat app that connects users based on relative location (ie. geohashing).

To connect groups of two or more people by location, you will need to take lat/long values and reduce the resolution of accuracy. By doing this, you can expand the coverage of proximity. You can use multiple resolutions at once or a fixed resolution.

Want to see geohashing in action? A full working demo can be seen here: Geohashing Chat by Proximity. Or check out the source code here: GitHub Repository for Geohashing Chat by Proximity.

geohashing chat

Basics of Geohashing

You’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Once you have, clone the GitHub repository, and enter your unique PubNub keys on the PubNub initialization, for example:

Next, we’ll cover some source code snippet for geohashing lat/long coords. It is fairly simple to increase the radius of lat/long position by reducing the accuracy of the float values. Using this you can expand the circle and collect a Lat/Long.

geohashing and user proximity

Geohashing Resolution “Zoom”

“Zoom” levels are basically several different cartesian grids with larger and smaller granularity. Once a Geofencing event fires, you will publish to a set of channels corresponding with each zoom level. Zoom levels are important as that is how we actually construct the link between Geofencing and PubNub. Zoom level is the resolution/de-resolution of the Cartesian coordinates Lat/Long (think X,Y coord).

By reducing the resolution of the lat/long coord we can construct a channel name that hits 1 box of the grid. Less resolution means larger the boxes and is required to determine a PubNub Channel that is associated.

This will create a very wide circle and generate a channel name used to connect. Next connect to PubNub with this channel name.

Proximity Chat

Multiplexing 9 Boxes

You will calculate the surrounding squares to extend the taper and radius in a way that provides more accuracy. This will remove the “fencing” effect.

Geohashing with PubNub Conclusion

That’s it! You simply reduce the resolution of a geo coordinate and use that as a channel name on PubNub. Also check out the browser’s navigator.geolocation.getCurrentPosition(...) method to acquire lat/long in a Chrome/Firefox/IE/Opera/mobile/Safari browser.

Geohashing with PubNub

Additional Resources

We have a real world, Internet of Things example in our Connected Car Solution Kit. With GeoFencing, trigger actions and alerts based on vehicle location and geographic boundaries (e.g. only vehicles within five miles get dispatch requests, customized traffic alerts, etc.).

Additionally, we have a tutorial and demo on building real-time geolocation apps with PubNub and JavaScript.

Get Started
Sign up for free and use PubNub to power geohashing

More From PubNub