Last week we released a new product, TMS MQTT. MQTT is a standard lightweight machine 2 machine messaging protocol. This enables the instant exchange of binary or text messages between code running on various operating systems. To fully take advantage of this, we developed our TMS MQTT client to work in VCL Windows applications, FMX Windows, macOS, iOS and Android applications and also on Linux desktop or Linux variants such as Raspbian on Raspberry Pi via FPC/Lazarus.
On this 22th birthday of Delphi, I wanted to show you how our TMS MQTT component allows you to develop a messaging application in a real RAD way. I’ll do this by implementing a chat application that allows you to chat between clients running on desktops or mobile devices. This effectively takes less than 40 lines of code using the TMS MQTT component of which 15 lines already go to inserting the chat text left and right in items of an FMX listbox and another +/- 10 lines detecting the origin of the chat text that is received to decide whether to show it left or right in the listbox. So, I’d say only about 15 lines of code effectively concern using the MQTT client to do the messaging.
For this demo, we use the Mosquitto MQTT test broker and connect to it with 2 lines of code:
TMSMQTTClient1.BrokerHostName := 'test.mosquitto.org';
Upon connecting, we subscribe to the topic /tms/chat/ that is the topic that will be used to exchange chat text between multiple clients. This takes 2 lines of code from the TMQTTClient.OnConnectedStatusChanged() event:
procedure TForm1.TMSMQTTClient1ConnectedStatusChanged(ASender: TObject;
const AConnected: Boolean);
if AConnected then
To send a message, text entered in a memo control is published on this topic with one line of code:
TMSMQTTClient1.Publish('/tms/chat/', TMSMQTTClient1.ClientID+'!'+ memo1.Lines.Text);
Here we add the unique ID of the app sending the chat text and the chat text itself.
Finally, incoming chat text on the subscribed topic is received via the TMQTTClient.OnPublishReceived() event. The unique client ID is retrieved to decide whether to put the chat text left or right of the message listbox and this is done via the code:
procedure TForm1.TMSMQTTClient1PublishReceived(ASender: TObject;
APacketID: Word; ATopic: string; APayload: TArray);
msg := TEncoding.UTF8.GetString(APayload);
vp := pos('!', msg);
if vp > 0 then
orig := copy(msg,1,vp-1);
alright := orig TMSMQTTClient1.ClientID;
msg := copy(msg, vp + 1, Length(msg));
And then comes the method to allows us to insert either left or right aligned text as items in an FMX listbox:
procedure TForm1.AddMessage(AMessage: string; AlignRight: boolean);
li := Tlistboxitem.Create(self);
li.StyledSettings := li.StyledSettings - [TStyledSetting.ssOther];
li.Text := AMessage;
li.Height := 22;
li.VertTextAlign := TTextAlign.taTrailing;
if AlignRight then
li.TextAlign := TTextAlign.taTrailing
li.TextAlign := TTextAlign.taLeading;
Compile and run this application on your operating system of choice. In my case, I did setup a small chat between the client compiled for Windows and the client deployed on an iPhone:
You can download the full source code of this FireMonkey application here.
I invite you to explore the numerous exciting capabilities of machine 2 machine messaging. Note that there
are JS libs that facilitate to do MQTT messaging via websockets from a webbrowser. The demo included in the TMS MQTT download is based on messaging between a desktop or mobile client app and a web page application. With the TMS MQTT client, you can also use machine 2 machine messaging from your Raspberry Pi and send this way various sensor data captured by the Raspberry Pi to desktop or mobile apps. In a follow-up blog, we’ll explain and offer such sample project.
I look forward to hear about how you will use MQTT machine 2 machine messaging in your apps or IoT projects!
… Read More