Titanium tip: How to rate limit geolocation callbacks

Rene Pot
All Titanium
Published in
3 min readJan 27, 2021

When working with Geolocation in Titanium you want an accurate location, at all times, but did you know the geolocation event can fire multiple times per second? Handling that amount of callbacks can strain a lot of parts in your app if you act on every single callback.

You can of course stop the geolocation event, and re-activate it when you want new location information, but this actually works against what you want to achieve: accuracy. When you stop the location event, the phone stops listening to location updates (unless another app is monitoring it too obviously). This causes the accuracy to drop, and if you reactivate it again later the first callback might (or will) return either inaccurate data, or outdated data.

So how should you handle this? Well… rate limiting (as you might’ve gathered from the title) is the way to go forward. What I always recommend is creating a library file for this, and let that always monitor location unless the app gets backgrounded, and just resume it when the app get’s forgrounded again. Generally, your lib should look something like this: (save this file as /app/lib/geolocation.js)

Now, this does not include requesting permission nor does it monitor app backgrounding/foregrounding, if you don’t know how to request location permission yet, check the docs.

So now that rate limit thing right? How does that work again? So right now in the setup as I have done above you see there’s nothing async here, and nothing is triggered by the location event. So technically, this is already rate-limited. Though, of course this is not the point of this blogpost!

Setting up multiple callbacks

So, what we’re going to do, is add a subscribe function to the library above, that can handle multiple callbacks with different rate limits. Because, why not right? Anyways, here’s how I’d do it (and add this function to your geolocation library file)

As you can see, we’re adding a subscription to an object and having a custom interval per subscription.

So now, we’re going to adjust the locationUpdate function

So this is a little complicated but you should be able to get it. Let me explain.

First, we loop through all subscriptions by looping through all items in the object. As we cannot natively loop through object properties we’re extracting the keys first (Object.keys()) and then forEach on it. Then, we check based on the current timestamp if it has been at least the “interval” amount since the last time we called that method.

So, to call this library and add a subscription, all you got to do is this:

And that’s it, now you got a callback handler that will fire at maximum every 5 seconds, but can of course be less frequently as location events are not firing based on a schedule.

And you might’ve wondered, why the name? Well… now you can control what subscriptions you have, and you could easily unsubscribe as well, from anywhere, without having to know what function it calls. And you could add logs to it and you’d know where it goes. It is versatile, and of course a personal preference, you could also work with arrays.

You could write an unsubscribe function like this.

That’s all you need to know, now you got a rate-limited, multi-callback central way of sending location notifications throughout your app.

--

--