B4J Tutorial [IoT] MQTT Protocol

Status
Not open for further replies.
What is MQTT?

"MQTT is a Client Server publish/subscribe messaging transport protocol. It is light weight, open, simple, and designed so as to be easy to implement. These characteristics make it ideal for use in many situations, including constrained environments such as for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts where a small code footprint is required and/or network bandwidth is at a premium.

The protocol runs over TCP/IP, or over other network protocols that provide ordered, lossless, bi-directional connections." (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html)

I recommend all developers to learn the main concepts of MQTT. It is quite simple to use and it solves many of the real-world issues that developers need to manage when building network based solutions.

You can find good tutorials about MQTT here:

Some of the differences between MQTT solutions and B4J server (http and websocket) solutions:
  • With MQTT we only need to develop the clients. The server (message broker) is already implemented for us.
  • Each client can send messages (publish) and receive messages (subscribe). Note that we can implement a similar solution with WebSockets.
  • It is a many to many connection. Clients can listen to messages from other clients.
Server / Message Broker

The clients connect to the broker. The broker is responsible for routing the messages and managing the connections.
You can either run a local broker (for local network solutions) or an online broker.

You can install and run Mosquitto: http://mosquitto.org/download/
Pay attention to the installation instructions as it depends on two additional resources. Follow the links in the installation dialog.

CloudMQTT is an online broker: cloudmqtt.com
They provide a free plan which you can use during development. Very simple to get started with.
Edit: You can embed the broker in your app with the new MqttBroker library (B4A + B4J): https://www.b4x.com/android/forum/threads/mqttbroker.61548/

B4X Code

  1. Initialize MqttClient with the server URI and client id.
  2. Set the options, including the user name and password, if required.
  3. Call MqttClient.Connect
  4. The Connected event will be raised. If Success is true then you can subscribe to topics and publish messages.
  5. The MessageArrived event will be raised whenever a message is received (based on the subscribed topics).
  6. Call MqttClient.Close to close the connection. Make sure to close the connection before you close the app or the process may keep on running in the background.
See the attached example. You will need to set the user name and password based on your CloudMQTT account or change it to connect to a local broker.

You can compile it in release mode and run multiple instances of the application to see how it works.
See the video here: https://www.b4x.com/android/forum/threads/mqtt.59346/

Note that the jMQTT library is compatible with B4A and B4J.
The library is available here: https://www.b4x.com/android/forum/threads/jmqtt-official-mqtt-client.59472/
B4i - iMQTT library: https://www.b4x.com/android/forum/threads/imqtt-official-ios-mqtt-client.59516/

A compiled example is available here: https://www.b4x.com/android/forum/threads/mqtt.59346/#post-374535
 

Attachments

  • MQTTExample.zip
    2.7 KB · Views: 5,585
Last edited:

DarkMann

Member
Licensed User
Longtime User

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for this library.
Have successfully tested on a Raspberry Pi after installing Mosquitto.
On the Pi started a terminal session and send messages, these have been received by a B4JClient App - made slight modifications to the sample app provided to receive temperature data. Messages pusblished from the B4J Client also received on the Pi.
This sample will be described in the next B4J HowTos update.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Some of the differences between MQTT solutions and B4J server (http and websocket) solutions:
  • With MQTT we only need to develop the clients. The server (message broker) is already implemented for us.
  • Each client can send messages (publish) and receive messages (subscribe). Note that we can implement a similar solution with WebSockets.
  • It is a many to many connection. Clients can listen to messages from other clients.

(Technology should stop, I start to be too old for all these news :D:(:))


I'm thinking if this MQTT can be useful (more useful) for my project (multiplayer turn based game).

1) "The server (message broker) is already implemented for us."
It should be an advantage but you need to change most logic of project, I suppose, and I fear I could have less control.

2) It is a many to many connection. Clients can listen to messages from other clients.
This is attractive, considering adding private chats to the game :)


What do you think about? Thank you.

[I suppose I can install my private broker on some VPS in the same way as a b4j server]
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
What do you think about?
What exactly is the question? I think that the best way to learn a new technology is by creating some small experiments. Once you understand it better you can decide whether you should implement your solution with a http server or MQTT. Note that you can also combine both.

I suppose I can install my private broker on some VPS in the same way as a b4j server
That's true.
 

LucaMs

Expert
Licensed User
Longtime User
What exactly is the question? I think that the best way to learn a new technology is by creating some small experiments.

Thank you for your answer, Erel.

I agree, do small experiments is always my preferred method; but then I am afraid that when you face a larger project, it is different.
You need to understand what is behind technology and I do not know if I have the necessary capacity and sufficient documentation.

I still have some doubts on how best to exploit a b4j web server (threads topic).

With this MQTT:
Clients can listen to messages from other clients.

I don't know how (obviously, I have not read the documentation at the time) but the broker is needed and then it must work for this messages exchange.
I have the feeling that this works as an office mailboxes, except that not only a single person has the right to read a message.
I have also the feeling that a web server, with its sockets, is something different.

Anyway, the most important statement is...

"I have not read the documentation at the time" :D


Thank you.
 

avalle

Active Member
Licensed User
Longtime User
What is MQTT?

B4X Code
  1. Initialize MqttClient with the server URI and client id.
  2. Set the options, including the user name and password, if required.
  3. Call MqttClient.Connect
  4. The Connected event will be raised. If Success is true then you can subscribe to topics and publish messages.
  5. The MessageArrived event will be raised whenever a message is received (based on the subscribed topics).
  6. Call MqttClient.Close to close the connection. Make sure to close the connection before you close the app or the process may keep on running in the background.

@Erel could you explain the difference between Connect and Connect2 methods? I'm asking because I'm trying to understand how to deal with "reconnections" in case the client disconnects from the broker. I tried using Connect2(mo) in the first initialization and then Connect() to reconnect without re-initializing user/password.
I get an exception when I call Connect(), so this may not be the right way to use it?

Thanks
Andrea
 

avalle

Active Member
Licensed User
Longtime User
Thanks Erel.
And does it mean that I also need to re-Initialize user/pwd or can I just save the MqttConnectOptions object and Connect2 by reusing it?

B4X:
Dim mo As MqttConnectOptions
mo.Initialize(user, password)
mqtt.Connect2(mo)
 

avalle

Active Member
Licensed User
Longtime User
In my B4A code using the MQTT library I am closing the MQTT object when Activity_Pause AND UserClosed = True.

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed = True Then
        If mqtt.Connected = True Then
            mqtt.Unsubscribe(mytopic)
            mqtt.Close
        End If
    End If
End Sub

I noticed that after mqtt.Close is executed I get the following message in the Log:

sending message to waiting queue (mqtt_disconnected)

What does it mean?
Is this the right place to use Close?

Thanks
Andrea
 

avalle

Active Member
Licensed User
Longtime User
Thanks Erel.

In this particular my case I'm not using a service because I don't need to maintain the subscription active if the app is put in the background.

In case a long running object would work better, do you suggest a "sticky service" or a "foreground service"?
 
Status
Not open for further replies.
Top