Archive

This morning, after loosing my broadband internet connection, I looked at my BB10 to see how I could tether it to my laptop, to share my mobile (3G) data connection. It worked a charm when connecting via USB – Bluetooth was another option.

It also has a handy indicator that shows how much data you are sending, and how long you are online, which matters quite a bit if you have a data cap.

If you are porting BlackBerry WebWorks apps from BBOS to QNX, you may notice that Html5 Geolocation does not work by default. The fix is quite simple, just edit the config.xml file and add the following XML snippet (below). With this in place, the alert shown opposite will shown on first load of your application, allowing the user choose whether he wishes to allow your app access to location information.

A simple way to refresh an image in Javascript is to use a setTimeout or setInterval, however, it doesn’t take into account how long it takes to load the image, so you could end up with a situation where a user on a slow connection does not load the image in time before the next image is requested.

Binding the load event to a image element with JQuery tends to fire prematurely, so that it does not give a fair reflection on when this image is loaded.

Say that you wanted to read data from a MySQL database, using PHP in a Linux environment, and then wanted to connect this data with a PhoneGap application, then you need to find a way to convert the MySQL data to JSON, to provide the interface with the PhoneGap app (which is basically HTML5)

So, as an example, I wrote a PHP script that connects to the MySQL database that underlies my OpenMerchantAccount.com blog, and converts the wp_posts table to JSON

I wanted to do something quite simple, plot tweets live on a map, that is, as people posted tweets live on twitter, drop a pin on the map, and display the tweet in a message bubble. I’ve done that, and popped it up on a website “http://geotweet.webtropy.com/” – It takes a few seconds to start up, but goes quickly then.

I used the twitter stream API, which is an unusual API, since once you call it, it doesn’t close the connection, just keeps pumping out data. I used the API call https://stream.twitter.com/1/statuses/filter.json?locations=-7,55,-6,56 – now, this does require a twitter username and password, but not any fancy OAUTH authentication.

The bounding box -7,55,-6,56 is roughly the area shown in the screenshot above, but some tweets land outside of this area, you may need extra filtering if this is important to you.

I needed to write a bit of .NET that could extract one tweet at a time from the stream. Each tweet is a Json object, and they are delimited by new line chars.

The source code for the web page is visible through view-source, so I won’t include it here. All I did was, initialise a google map. Once the map was ready call my AJAX function above. extract either the geo or bounding_box object from it, and plot a marker on the map at that position. Once that is done, the Ajax is called again, and so the cycle continues.

***Update***

So, after a day of testing this new site, I suddenly got hit with a “402 client error” from twitter, which is funnily described as “Enhance your calm” on Twitter’s documentation. Basically, you’re not supposed to connect and disconnect rapidly to the stream API, you are supposed to keep connected to the stream, and only reconnect if you have to.

Fair enough, but it requires quite a redesign for the code.

What you need to do, is kick off a thread from Global.asax on Application_Start like so:

I had to modify the “FireHose” class to have a new method called ReadConstantlyFromUrl, which opens a connection, reads a line at a time for the stream, setting a public static variable, and only disconnecting on error, or if the end of the stream is reached. I also added a cool off period of a second, so that the reconnection attempts wouldn’t bunch up.

The Thread.Sleep allows the Ajax handler wait until a new tweet has arrived before writing to the output stream. Do note, that if there were more than one client visiting the page at the same time, then it would be a first-come-gets-the-tweet. Which is not ideal for most situations, but ok for my test.

**Update**

Working on a solution for multi-client access, so that one client would not “steal” the tweets that should be visible to all clients, I decided to add an extra field in the Ajax called “IgnoreTweetIf”, this is set to the length of the tweet (the full data, not the text only), and the Ajax handler will not return a tweet if the length of the last tweet it has matches the length of the tweet that the client last received. On first call this “IgnoreTweetIf” is set to zero, so that any tweet is returned.