Cross platform messaging made easy with TMS MQTT

Bookmarks: 

Tuesday, February 14, 2017

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:
begin
  TMSMQTTClient1.BrokerHostName := 'test.mosquitto.org';
  TMSMQTTClient1.Connect();
end;

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; AStatus: TTMSMQTTConnectionStatus);
begin
  if AConnected then
    TMSMQTTClient1.Subscribe('tms/chat');
end;
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);
var
  msg,orig: string;
  vp: integer;
  alright: boolean;
begin
  msg := TEncoding.UTF8.GetString(APayload);

  vp := pos('!', msg);

  if vp > 0 then
  begin
    orig := copy(msg,1,vp-1);
    alright := orig <> TMSMQTTClient1.ClientID;

    msg := copy(msg, vp + 1, Length(msg));
    AddMessage(msg, alright);
  end;
end;

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);
var
  li: Tlistboxitem;
begin
  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
  else
    li.TextAlign := TTextAlign.taLeading;

  listbox1.AddObject(li);
end;

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!



Bruno Fierens


Bookmarks: 

This blog post has received 8 comments.


1. Tuesday, February 21, 2017 at 1:56:57 PM

Amazing!
Is it also possible to use the "payload", for sending binary data like pictures?

Thank you!

Matthias


2. Tuesday, February 21, 2017 at 2:41:10 PM

Hi Matthias,

Yes. that is possible!
In this example we use the publish function to send a string, but you can also pass it an array of bytes. So sending images is no problem.

Kind regards,
Nick

Nick Vierstraete


3. Tuesday, February 21, 2017 at 4:09:12 PM

Thank you!

Another nice feature would be a fully threaded component... :-)
Or is it already?

Matthias


4. Wednesday, March 01, 2017 at 9:35:42 AM

Internally, this component is indeed already fully threaded

Bruno Fierens


5. Wednesday, March 01, 2017 at 9:36:50 AM

In our February knowledge-base newsletter, there is an example for how to send an image with MQTT: http://www.tmssoftware.com/site/kb/KB_20170228.htm#9

Bruno Fierens


6. Wednesday, March 01, 2017 at 10:03:48 AM

Dear Mr. Fierens !

TMSSoftware has created a nice component - TMS MQTT.
This is what you need for remote diagnostics and receive
information from the remote object.

However, the following questions exist :

1. You allow to download and install a Trial version this component.
But it is set to Win32 and not allows you to install
your component ( and your example, MyLocation,TMSQuickAndEasyChat)
in another operating system, such as Android.
How to make so that we can examine your examples in
Android OS through a Trial version of the component.

2. TMSQuickAndEasyChat your example does not work under Win32 and Android
although compiling the project succesfully.
Probably, it is connected with the name of the broker?
In your example, you use the broker
TMSMQTTClient1.BrokerHostName := ''test.mosquitto.org'';
Where did you get the name of the broker and why do not use
HiveMQ, which do you recommend?


3. The user should always use a broker
HiveMQ or can use another freeware broker. If so what ?
What actions he should perform to sign up with a broker?

Please answer these questions

With respect.

Alexander Ryss
(ab.ryss@yandex.ru)
Moscow.
/1.03.2017/



Ryss Alexandr


7. Wednesday, March 01, 2017 at 10:48:37 AM

Sorry !
Example - TMSQuickAndEasyChat compile and running on Android OS at Trial version this component !!!

Ryss Alexandr


8. Wednesday, March 01, 2017 at 11:25:51 AM

First of all, can you contact us via regular support channels http://www.tmssoftware.com/site/support.asp as blog comments are not an ideal way to deal with support communication.

1. The trial includes support for Windows, macOS, iOS, Android. If you experience an issue, please share the details via regular support contact so we can investigate and assist.

2. Please define "not work". Is there a connect error? Other? Free brokers can be found here: https://github.com/mqtt/mqtt.github.io/wiki/public_brokers You are free to choose. We just used HiveMQ for the Location demo as this broker has both regular socket & websocket support.

3. You can install a (free) broker yourself. There are many available. https://github.com/mqtt/mqtt.github.io/wiki/servers

Please contact us by email for further followup if you need more help.

Bruno Fierens




Add a new comment:
Author:
Email:
  You will receive a confirmation mail with a link to validate your comment, so please use a valid email address.
Comment:
 
Change Image
Fill in the characters from the image above:
 

All fields are required.
 




Previous  |  Next  |  Index