Real-time Chat Blog

Geohashing Chat by User Proximity Tutorial

3 min readStephen Blum on May 7, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

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

Can Empathy Exist in the Metaverse
News May 16, 20221 min read

Can Empathy Exist in the Metaverse

A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.

PubNub Staff

PubNub Staff

How to Advance Telehealth and Virtual Care Technologies
News May 2, 20221 min read

How to Advance Telehealth and Virtual Care Technologies

Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...

PubNub Staff

PubNub Staff

Another Step Towards Data Security: ISO-27001 Implementation
BuildMay 2, 20221 min read

Another Step Towards Data Security: ISO-27001 Implementation

Today, we are glad to announce that we are currently in the process of implementing ISO-27001 security standards.

PubNub Staff

PubNub Staff