B4J Tutorial [IoT] MQTT Protocol

Discussion in 'B4J Tutorials' started by Erel, Oct 19, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:
    http://www.hivemq.com/blog/mqtt-essentials/page/2/ (part 1 and 2)
    http://www.hivemq.com/blog/mqtt-essentials/ (parts 3 - 10)

    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): http://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.
    It depends on jRandomAccessFile v2.10+: http://www.b4x.com/android/forum/threads/updates-to-internal-libaries.48274/#post-373825
    You can compile it in release mode and run multiple instances of the application to see how it works.
    See the video here: http://www.b4x.com/android/forum/threads/mqtt.59346/

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

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

    Attached Files:

    Last edited: Dec 20, 2015
    fredo, jmon, AzureCrystal and 8 others like this.
  2. DarkMann

    DarkMann Member Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

  4. DarkMann

    DarkMann Member Licensed User

    Erel likes this.
  5. rwblinn

    rwblinn Well-Known Member Licensed 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: Oct 19, 2015
    inakigarm, Erel and Mark Turney like this.
  6. LucaMs

    LucaMs Expert Licensed User

    (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: Oct 21, 2015
    Mark Turney likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    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.

    That's true.
     
    jinyistudio likes this.
  8. LucaMs

    LucaMs Expert Licensed User

    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:
    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.
     
  9. b4auser1

    b4auser1 Active Member Licensed User

    There is Starter Service - Consistent & Single Entry Point


    Is it possible to implement "Finilizer" Sevice - single exit point ?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    In B4J you can use the MainForm_Closed event.

    In B4A it is less important as the connection will be closed when the process is killed by the OS.
     
    Mark Turney likes this.
  11. avalle

    avalle Member Licensed User

    @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
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    The difference is that Connect2 allows you to change the default settings. If your broker requires username and password then you should always use Connect2.
     
    valentino s likes this.
  13. avalle

    avalle Member Licensed 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?

    Code:
    Dim mo As MqttConnectOptions
    mo.Initialize(user, password)
    mqtt.Connect2(mo)
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    You can reuse it.
     
  15. narek adonts

    narek adonts Well-Known Member Licensed User

    I think we need a Server library for MQTT broker like jServer to be able to create a full server with SQL access,etc...
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    This is not how MQTT works. You don't need a server library.

    Create a B4J app that listens to all the relevant topics and do whatever you need to do (access the database for example).
     
    valentino s likes this.
  17. avalle

    avalle Member Licensed User

    In my B4A code using the MQTT library I am closing the MQTT object when Activity_Pause AND UserClosed = True.

    Code:
    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:

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

    Thanks
    Andrea
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    Usually it is better to use a service for any long running "object".

    Your code is fine. It just means that the Disconnected event was raised when the activity was paused.
     
  19. avalle

    avalle Member Licensed 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"?
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    It depends on your requirements. However as this is a B4J thread it is better to further discuss it in the Android questions forum.
     
Loading...