Tag Archives: development

Alpha, beta… [aka try stuff for free!]

A couple of small IBM Software announcements, depending on your point of view :-)

  1. WebSphere Application Server version 8.0 is now available to try out as an alpha version. This was announced over on the WebSphere Community Blog a couple of weeks ago (a blog worth following, if you are into WebSphere). Key parts of JEE 6.0 are showing up, and there are new security and governance features, as well as a simplified install. Check it out.
  2. Rational Software Architect is now available as an open beta. This is billed as offering “a simpler, more streamlined way to design, develop, deploy, and test IT solutions”.

And now back to your regular unscheduled programming.

Experiments with PHP and MQTT

Over the past few days I’ve been playing around with combining lightweight messaging and PHP. There are a couple of reasons for this, but the primary one is that I’d like to extend my prototype iPhone CurrentCost monitoring web application to display more up-to-date information about the state of my home energy usage. I’d planned to do this for a while, but recently Mark Taylor created his own version of the iPhone interface (PDF link) and he has got current readings on the front page. Clearly, I have to compete :-)

Actually, in my system, I’d like to do things a different way. The heart of my setup is a Really Small Message Broker. At the moment, data from the CurrentCost meter comes in over the USB connection and is then published in pieces, or on topics, to the RSMB (temperature and energy readings are separate). These published messages are then read by a script which is subscribing to the topics and squirrelling the historical data into an rrdtool database; and also being pushed up to our IBM broker “in the interweb cloud” via an MQTT broker bridge connection.

So in theory, having the up-to-date information in the web UI should be a simple case of grabbing the MQTT publications on each topic and displaying them. The way I’ve coded things (and would prefer to do things), this involves having the ability to subscribe to MQTT publications from PHP.

I’m not at the end of the road yet, but I do have a starting point.

howitworks.png

I’ve got a front-end test page which currently uses Prototype to send an Ajax request to a server-side PHP script (yes, I have had jQuery recommended to me, and I may well look into that instead of Prototype, but this works).

The server-side script uses the Simple Asynchronous Messaging PHP library. SAM is a wrapper which enables a variety of messaging transports to be supported in PHP, such as MQTT, WebSphere MQ or WebSphere Platform Messaging. Just one thing: I found that in order to get the most recent SAM release to work on Ubuntu on my MPC-L, I had to install IBM’s XMS client SupportPac (for some reason, it won’t build without it, even though it is “optional”) and I also had to delete a spurious empty line from the end of /usr/share/php/SAM/php_sam.php to prevent header issues. Other than that, it was all good.

The script is really simple and basically uses all of the defaults to create a connection to my local RSMB over MQTT. The advantage of this being server-side is that I don’t have to open my RSMB to the Internet, the PHP code can connect via localhost. Once that’s done, it creates a subscription on the topic I’ve asked for, and receives the first data that comes along, then echoes it back to the front-end. I could make it auto-updating with Ajax.PeriodicalUpdater too, but there’s no need to put a load on my server.

Wanna see a quick demo? ;-)

I’m quite pleased with the way this is working. There’s some more plumbing to do, and I’ll almost certainly extend the server-side piece to allow two-way communications (publish as well as subscribe) as well as finer-grained control over the options. As a proof-of-concept though, I think this is looking good.

TwtrCtr – Tracking Twitter followers with an iPhone

twtrctr logo I’ve already mentioned this on Twitter, but so far I haven’t had a chance to write about the culmination of my first “proper” efforts to create a mashup: TwtrCtr.

For those not obsessed with Twitter or their follower numbers, there’s an application you probably haven’t heard of called TwitterCounter. The site provides tools, graphs and APIs for charting the growth of a user’s Twitter followers. It’s interesting, and if you’re slightly obsessive about figures and have a belief that such things “matter”, then you might already spend time checking it and looking at the pretty graphs. It also provides an element of “prediction” based on historical growth trends, and does cool things like allowing users to compare themselves to others and watch the graphs intersect or overlap.

twittercounter website There’s just one issue – although it looks great on the desktop, the site isn’t laid out very well for mobile browsers like Mobile Safari on the iPhone. It also uses a Flash component to display the graph, and since there’s currently no Flash on the iPhone, you get a big blank box in the middle of the screen.

The nice part is that TwitterCounter has a simple REST API which enables a developer to get the raw data about a user’s follower numbers. If you use Twitter clients like Tweetdeck, then you’ll see this in action.

Based on my previous experience of using the iWebkit framework to build an iPhone-optimised web wrapper for the data from my Current Cost meter, I thought it might be interesting to play around with using iWebkit to display the TwitterCounter stats.

iWebkit is a simple HTML framework which provides a set of CSS classes to make your web pages look like native iPhone apps. There are other frameworks out there like iUI and webapp.net, but they depend a lot more on knowledge of AJAX and some more advanced / dynamic coding, whereas iWebkit is all about simplicity – if you know your HTML basics, it is pretty straightforward. As it happens, you can extend it very easily as well – in my case, I combined it with some PHP functions which call out to TwitterCounter and echo the numbers into a table on a web page.

Although I worked with PHP a lot a few years ago, I hadn’t really done much with the language recently. The first thing I did was to create a simple piece of code to call the TwitterCounter API and get back the data for a specific user; and then I displayed it in a web page. Once I’d done that, it was pretty easy to get the whole thing wrapped into two pages of iWebkit template code, and style it all appropriately.

twtrctr mark 1

Revision one of the interface and app ran off my home server, and didn’t look fantastic. My initial thought for a name was “TwitterCounter nano”, but I changed it after realising that it didn’t fit so well as a name on the iPhone home screen :-)

In the screenshot on the left, you’ll also see that the first form I came up with was far from “iPhone native-looking”… the current version of iWebkit didn’t have form CSS classes, so I had to tweak things a little. I also found it was worth digging into the Apple Web Development Guidelines for the iPhone, which gave some hints about how to make some iPhone-specific tweaks like turning off auto-capitalisation for the text entry box (since most usernames are all in lowercase), and how to add a text hint, for example.

twtrctr mark 2 So, the second iteration of the UI looks a lot nicer. It’s also possible to hit the bookmark button in Safari and add the app as a shortcut on the iPhone home screen… (see the icon at the top of the post)… if you do that, and launch TwtrCtr from there, it will act as a full-screen iPhone app with no Safari controls, giving a much more native experience. The user can then navigate by using the controls in the header bar rather than the forward and back buttons provided by the browser.

I added an FAQ page, too, so if you want to know more about the app you can check that out directly on the site. In these days of heightened concern about Twitter security I also thought it would be a good idea to add a note on the front screen to point out that it doesn’t ask for anything more than a username, and it doesn’t log that anywhere, it just passes it on to the TwitterCounter API.

twtrctr display OK, that was a rather long explanation of the evolution of the first page! The important part is actually how the data is displayed. Once you’ve entered a valid Twitter username and hit “Get User Stats”, you get a single-page representation  of the TwitterCounter data for the given user.

The top 2 or three rows are links which will open the user’s profile page, homepage/URL (if one is set… otherwise that row doesn’t display), or display a simple graph / chart which is generated by the Google Chart API. The latter is something high on my list of enhancements, because it looks a little dull at the moment; also, I’m generating the Google Charts URL myself rather than using one of the PHP wrappers to the API, which would probably be a lot simpler.

Don’t look too closely for the rough edges… right now, it doesn’t actually make any effort deal with cases where a user doesn’t exist, or TwitterCounter or Twitter is down…. I know about those small issues :-)

In a nutshell, then – that’s all it is. A mashup which allowed me to explore a bit more iPhone-specific web development, some PHP / REST / XML coding, and a bit of Google Charts stuff as well. I have to say that the TwitterCounter folks (Boris and Arjen in particular) have been brilliant and very helpful and supportive, despite their app having an issue with the Twitter API while I was in the middle of developing this mashup on top of their API! iWebkit is a lot of fun to play with, and very simple as well – I know the developers are working hard to add new features into that framework whilst keeping it simple and aiming it at “non-techies”.

Oh, and incidentally, although the stylesheets make it look like an iPhone app, it should work perfectly well in any desktop or mobile browser – it’s plain old HTML.

I’m not making any claims about how this might develop in the future, but I’ve got a couple of ideas for tweaks that I might make. In the meantime, if you’re an iPhone (or other mobile device) Twitter user, do take a look at http://andypiper.tv/twtrctr and see how it works for you. Let me know what you think, or any ideas for additions you might find useful! Feel free to follow me on Twitter and @ your suggestions and comments to me, too.

Update: TwtrCtr is now linked directly from the TwitterCounter home page! Follow the iPhone link in the page footer! :-)