MQTT Client Library for Delphi

Introduction:

TMQTTClient is a non-visual Delphi Client Library for the IBM Websphere MQ Transport Telemetry protocol. It allows you to connect to a Message Broker that uses MQTT such as the Really Small Message Broker which is freely available for evaluation purposes on IBM Alphaworks. The specification for the protocol can be found on the WebSphere MQ section of the IBM Documentation site.

Limitations:

This is not a reference implementation of the MQTT Protocol but does support both Publishing Messages and Subscribing to Topics with the following limitations:

  • Supports versions of Delphi =< Delphi 2007 at the moment, Delphi 2009 introduced official support for Unicode and requires a little more work.
  • It only allows and supports QoS 0 Messages. I haven’t built QoS levels 1 or 2 in yet as I personally have no need for them but this is planned for future versions.
  • You are required to schedule pinging the server yourself (using a standard TTimer for example). The client library implements a ping command but doesn’t automatically ping the server itself at regular intervals.

Note: You should also be aware that it uses part of the Synapse Internet Communications Library for its Socket support so you’ll need to ensure that this is available on your search path.

Download:

You can find the source and download a packaged zip from TMQTTClient on Github.
You can download the TMQTTClient and Sample version 1.01 client here. TMQTTClient.zip (16Kb).

Latest Changes:

1st March 2009: Version 1.01 includes a few updates and few new features – see CHANGELOG.txt for more information.

Usage:

There is a sample project included in the download but usage is relatively simple. This is a non-visual component so all you need to do is to put MQTT.pas and MQTTReadThread.pas  into a directory that is in your compiler paths and then put MQTT in your uses.

uses MQTT;

var
	MQTTClient: TMQTTClient;
begin
	MQTTClient := TMQTTClient.Create('localhost', 1883);
	MQTTClient.Connect;
	MQTTClient.Publish('/home/kitchen/temperature', '18 degrees celsius');
	MQTTClient.Disconnect;
	MQTTClient.Free;
end;

Subscribing to a topic is just as easy and you can create an event handler to handle incoming Publish messages on any particular topic:

uses MQTT;
var
	MQTTClient: TMQTTClient;

procedure TmyForm.btnConnectClick(Sender: TObject);
begin
	MQTTClient := TMQTTClient.Create('localhost', 1883);
	MQTTClient.OnPublish := OnPublish;
	MQTTClient.Connect;
        MQTTClient.Subscribe('/home/kitchen/temperature');
	MQTTClient.Publish('/home/kitchen/temperature', '18 degrees celsius');
end;

procedure TmyForm.OnPublish(Sender: TObject; topic, payload: string);
begin
	memoStatus.Lines.Add('Publish Received. Topic: '+ topic + ' Payload: ' + payload);
end;

Licensing:

This code is publised under the MIT-License:

Copyright (c) 2009 Jamie Ingilby

delphi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Future Plans:

In no particular order:

  • Try to get it compatible with Freepascal so it can be compiled on Linux and the many other platforms that Freepascal supports.
  • Implement QoS support for Levels 1 and 2 (might be a long way off for now!)

Special Thanks go to Andy Stanford-Clark and Nick O’Leary for their help on various “hiccups” that I encountered while developing and testing TMQTTClient.

If you are using my TMQTTClient in anything then I’d love to hear about how you’re using it, please contact me and let me know what you’re using it for.