Wednesday, December 17, 2014

MQTT to Message Broker IIB

So now that I have MQTT working on a WebSphere MQ Queue Manager, and once you figure out how to do it it is simple.  The next step is to subscribe to those MQTT topics in order to have queues read from them.

The gist of it is that you create a new local topic first, using the MQ Explorer, When you create that topic, the topic string in the topic has to match the topics that are being published to MQTT.  My MQTT clients in this demo are publishing to the topic /World.  So if I want to set up an MQ Topic that mirrors that, I just say its topic string is /World. No need for a subscription between MQTT and the MQ Topic.  MQ just magically maps between the two.

Then you create a subscription to that WebSphere MQ topic from a local queue, and then MQ has access to that queue.

The Message Broker App I've written reads the queue and write to a file.  It looks like this, just a queue input node and a file writer output node, tied together.  I set the message parser to JSON just in case I start to process messages in some later part of this.






Tuesday, December 16, 2014

Getting Comfortable with BlueMix

So today I wanted to take my existing web application / demo that I've been working on, and move it up to BlueMix.  I currently have the project stored in an eclipse workspace on my hard drive as an eclipse project.  What I want to do is store that project in BlueMix.

The first step is to create a project in BlueMix.  There are a few ways to do this.  You can start with a pre-built template.  Or you can just create a project.  A pre-built template has some advantages, but also has a learning curve.  You have to understand what the template is providing and how to modify it to meet your needs.

So I'll go with an empty project.  From BlueMix. Click on My Stuff. From there click on Create Project.  This will create an empty project and give you a link to a git repository.

Back on the workstation, go to your eclipse project folder.  From the command line do a 'git init', and then a 'git add -all', and finally a 'git remote bluemix {add the url to your bluemix repository}'.  Then 'git commit', and 'git push bluemix master', and your project will be up in JazzHub where you can share it with others.

At this point though, there's no build process for your project up in BlueMix.  That means, while all of your stuff is technically up there, BlueMix does not know how to build and deploy it.  Hmmm... that's a problem for another day.  

MQTT on WebSphere MQ

I wanted to use MQTT hosted on WebSphere MQ for my project.  It is an add on. Although it is shipped with the product, it is not installed by default.  So, the first step is to fire up the MQ installer Setup.exe and select "Maintain or upgrade an existing instance", and add the two MQTT components.

After that, you need to run a script to create an MQTT queue manager, and a couple of channels.  One for MQTT traffic, the other for HTTP WebSocket traffic.  They provide a sample file, but it does not include the HTTP WebSocket channel.  So, you'll have to add that.

The Sample file is located here:  C:\Program Files (x86)\IBM\WebSphere MQ\mqxr\samples
and it is called SamplesMQM.bat  or .sh if you're of that ilk.

Copy it, and add these lines at the bottom:
rem Create (and start) a channel
echo DEFINE CHANNEL('WebSocket') CHLTYPE(MQTT) PORT(8000) PROTOCOL(HTTP) MCAUSER('Guest') | runmqsc %QMGR%

While you're in there you can give your QMGR a better name.

Run that batch file, fire up your MQ Explorer, Start your Queue Manager, and you're ready to rock some MQTT traffic.


Working with Git

So today I decided that I want to get with Git, the distributed software version control system that's taken over by storm.

The easiest way was to download Git from their git-scm.com.  They have great tutorials, and you're up and running in minutes.  I wish I had this back when I was doing development.  It has certainly helped with my simple projects this week.

IBM has their Git repository at jazzhub.  Google has theirs.  That makes it easy.  Both IBM BlueMix and Google GCloud, when you create a new project, give you the link to the Git repository for that project.  After that all you have to do is clone it, write code on your machine, commit, and push your code up to the cloud and after a build, it's working.

eGit is a nice add on for eclipse, but you have to watch eclipse versions.  I had some trouble with a missing Apache HttpClient, so it took a little work.  But basically you don't really need it, as long as you're OK with command line.

So, Hourray for Git.

Monday, December 15, 2014

Demo on Blue Mix

I decided to try BlueMix today.  Actually I've used it before to develop Rule Services for some of our demos and proof of concepts.  Today I'm going to see if I can take what I did for the MQTTMaps demo and have it run in BlueMix.  I'm guessing it is not too hard.

1) Get a BlueMix Account - they're free for the first some number of days, so that was easy.  BlueMix.com

2) Pick a starting point - Since this is going to be a web app I'll start WebSphere Liberty Profile.

3) Add Git to the project - this basically gives me a way to develop the code on my machine, sync it with what's in the cloud, and run it in a matter of seconds.  It uses jazzhub, IBM's version of git.  So I'll do a git clone https://hub.jazz.net/git/ibmdthompson55/MQTTMaps in to my local file system.

4) I need something called Cloud Foundry... something new to learn...

Google Maps with MQTT Demo

I wanted to write a quick demo, where I could have everyone in the audience click locations on a map, and send those locations to a central server where I could process them and show aggregations, analytics, event processing, and some aspects of mobile.

The basic idea is to show a map of the U.S.  Then let my customers/friends/coworkers/wife click on it.  Each click, I will tell them, represents a transaction at a store.  My back end server will collect all of the clicks as map locations from all of the people in the room.  And from that information I will start to draw interesting stuff on the projector, from my computer.

To start with I will need some place to host the application, and for that I hope to use IBM's Cloud, possibly BlueMix or Maybe on my desktop using some flavor of WAS.

I'll need a source repository, and for that I hope to use Git.

I'll need to display maps on a browser, and for that I'll use Google Map API.

To gather all of those mouse clicks I will use MQTT, which can scale up to hundreds of thousands of clicks per second, if it is run on IBM hardware.

I'm going to add AngularJS, and Twitter BootStrap, as I will probably need them at some point.

WebSphere Liberty App Server --
This is just a great App Server.  It starts in a couple seconds, and it has full fidleity with its big brother WAS ND, should I ever what high availability and failover.  I downloaded it from WASDev, and configured a server tu run on port 19080.  In my eclipse tooling (I'm actually using IBM ODM Rule Designer for this because I happen to have it already set up) I just create a dynamic web project, and do an export of the WAR file to the DropIns folder of WebSphere Liberty.  It deploys immediately.

Map Pinner --
I started with Google Maps in an html file. Really simple, they provide a lot of examples.  You need to sign up for a API_KEY so they can track your map usage.

Relatively quickly I was able to put up a map that responds to mouse clicks, and draw markers where the mouse was clicked.  Next I just have to send those events to the server, and from there feed them into the magic software layer.  So far, because everything is client side.  HTML5. I just have all of this in my index.html.


To set markers, you need to add an event listener to the map, and in the callback function, that's where you place the markers, and call MQTT with their location.

I added approximately the following JavaScript  

var mapOptions = {
center: { lat: 41.748789, lng: -72.543329 },
zoom: 8 };

var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});

function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
title: "Marker #"+markerCounter ,
map: map
});
}

MQTT --

I'm using the opensource Paho MQTT client, which was originally developed by IBM.  There's a decent code sample, which I'll just add to the init code and then send location information in the placeMarker function.

So I just copy and paste the Paho file into a javascript include called mqttws31.js  and set some init variables.  I'll use localhost for now.  And the functions onConnectionLost and onMessageArrived are just straight out of the sample code.  I'll need to include those as well.

        var hostname = "localhost";
        var port = 8000; // the mqtt port is 1883, but we'll be using websockets to get there on 8000
        var clientid = "mapDemo_" + parseInt(Math.random() * 100, 10);
     
        //

        client = new Paho.MQTT.Client(hostname, Number(port), clientid);
        client.onConnectionLost = onConnectionLost;
        client.onMessageArrived = onMessageArrived;

MQTT Server --

I happen to have IBM MQ 7.5.0.1 on my desktop already, part of my IBM Integration Bus installation, so I'll use that.  I need to modify my MQ install in order to add MQTT functionality.  This is well documented in the Knowledge Center and takes a few minutes.  Acquiring WebSphere MQ requires licensing, beyond what a casual web developer might have access to.

But once you've done that, there's just a simple script to create an MQTT and WebSocket ports, MQ apps can access the data in the MQTT queues, which is kind of neat.

If you don't need to integrate with things outside of MQTT, then HiveMQ is a simple, no-cost for limited use, MQTT server.  Pull it down, unzip it, edit the configuration.properties files and set websockets.enabled=true.  Then start it from the bin folder.  Could not be simpler.  You still need to configure the WEbSocket port though, but it's simple.

Map Listener --
Again, this is a simple Google Maps API, running in html5.  The difference between the this and the Map Pinner, is that it listens for MQTT messages from the Map Pinner, and drops pins in it's own map, taking input from everyone who's dropping pins.  It makes a nice little MQTT demo.

Map Analytics --
Once we're getting a stream of map data we can do some Map Analytics.

Map Event Rules --
Once we're getting a stream of map data we can do some Map Event Rules, and looks for events that take place outside for the norm.

More to come...


 


















Put your Android Phone into Developer Mode

I am working on an HTML5 app.  It works on my browser but doesn't work on my phone.  What ame I to do?  Simple, set the phone to developer mode and connect it to a remote debugger on my laptop.

In the settings, scroll down to the software section.  From there find the build number.  Tap on that 5 times.  Eventually a dialog will pop up telling you you are a developer.  Got back to settings page and you'll find the Developer{} section is at your disposal.