Discussion in 'Chit Chat' started by Erel, Oct 15, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    MQTT is a great network solution.

    I never worked with it before. After trying it for a few days I think it is a very useful feature. It solves many of the difficult parts developers need to address when building projects that involve multiple machines.

    An official B4J library which is also compatible with B4A will be released next week. There are already libraries for MQTT in the forum:
    However as this is an important feature I decided to provide an official library.

    Here is a video of a small B4A / B4J example:

    Note that the latency on the Android device comes from the screen cast.

    The main code of this example:
    Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
    Dim obj As Object = serializator.ConvertBytesToObject(Payload)
    If obj Is CircleData Then
    If Topic = mytopic Then Return 'the circle was already drawn
         Dim cd As CircleData = obj
    Else 'obj is string
         Dim s As String = obj
    Select s
    Case "clear"
    00, Canvas1.Width, Canvas1.Height)
    Case "close"
    End Select
    End If
    End Sub

    Sub Canvas1_MouseDragged (EventData As MouseEvent)
    If mqtt.Connected = False Then Return
    Dim cd As CircleData
       cd.x = EventData.X
       cd.y = EventData.Y
       cd.clr = 
    Rnd(0x80000000, -1)
       mqtt.Publish(mytopic, serializator.ConvertObjectToBytes(cd))
    End Sub
  2. woniol

    woniol Active Member Licensed User

    That is great news to have official MQTT library for B4J and B4A.
    What mqtt broker was used in this demo?
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. moster67

    moster67 Expert Licensed User

  5. Beja

    Beja Expert Licensed User

    As always, Erel,

    you are great.......... Thanks a million
  6. LucaMs

    LucaMs Expert Licensed User

    Fortunately I do not know that MQTT, otherwise I might think that it is very useful for my B4J web server - B4A client, for which I am doing somersaults and I might even think that I wasted time, while using this next library everything will be more simple.

    Just kidding (more or less :D)
    Last edited: Oct 16, 2015
  7. LucaMs

    LucaMs Expert Licensed User

    It is 5 am (it means: I'm sleeping :D) and I have found documentation in English only.

    As I understand this protocol has advantages in bandwidth and energy saving (?!).

    Other advantages?

    Should I prefer this and give up web server b4j "tools"?
  8. Erel

    Erel Administrator Staff Member Licensed User

    LucaMs likes this.
  9. b4auser1

    b4auser1 Well-Known Member Licensed User


    There is a Cloud based MQTT: CloudMQTT
    It is based on Mosquitto and is hosted on AWS.
    There are several plans including free one.
    The service supports MQTT over SSL, WebSockets.
    There is authentication (username/password) and authorization support(ACL).

    It would be great if you provide information in the B4x MQTT tutorial (and samples) how to use ClouDMQTT (SSL, Websockets, authentication and authorization).
    Thank you in advance.

    P.S. On the CloudMQTT site there is a list of clients which support CloudMQTT: java, ..., php and etc. I believe the B4x should be in the list as well.
  10. b4auser1

    b4auser1 Well-Known Member Licensed User

    MQTT supports other ports than not HTTP/S and it could be a potential issue. In some organizations the firewall rules restrict allowed ports to 80/443 only. Though if MQTT becomes the base protocol for IoT the standard for firewall rules can be adopted.
  11. LucaMs

    LucaMs Expert Licensed User

    Thinking (just one minute :)) on how to implement your example using b4j-web socket-http... I should build a server and I have the impression that you did not have to build a server. Is that "broker" a server?

    Can you post the code for connection?

    Thank you
  12. b4auser1

    b4auser1 Well-Known Member Licensed User

    MQTT Broker is a server which implements communnications between different clients:
    F.e. an B4A/b4J app publishes something, and MQTT Broker delivers messages over WebSocket to web browser or vice versa.

    Take a look at presentation link in the post of Erel above or at the documentation

    P.S. I am really interested more in the security settings
    , but I believe many people are interested in WebSockets as well.
    LucaMs likes this.
  13. coslad

    coslad Well-Known Member Licensed User

  14. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    1. Download:
    2. Double click on the jar. You should see this window:


    Now draw something. The drawing will show on every instance of this app (worldwide).
    You can open two instances to see it.

    It uses CloudMQTT. Note that the free plan which is the one used here supports up to 10 concurrent connections.
    Last edited: Oct 19, 2015
    billzhan and inakigarm like this.
  15. coslad

    coslad Well-Known Member Licensed User

    Hi Erel, if your post is the answer to me, i meant to create the server broker on Android. If not, excuse me
  16. Erel

    Erel Administrator Staff Member Licensed User

    Sorry for the confusion. It is not related.
  17. Mark Turney

    Mark Turney Active Member Licensed User

    Very cool stuff here Erel. Thanks for the example. As soon as I make some headway with learning this, I'll create another video for you :)!
    AlbertDE and Erel like this.
  18. cxdzbl

    cxdzbl Active Member Licensed User

    error connecting

    I registered the user on the CloudMQTT, modified the demo ( code (user name, password, server, port), but also appeared I have no permissions, I hope to give a detailed operational steps (b4a)

    Attached Files:

    Last edited: May 16, 2017
  19. Erel

    Erel Administrator Staff Member Licensed User

    Please don't make duplicate posts.
  20. cxdzbl

    cxdzbl Active Member Licensed User

    After continuous testing, chat_b4a is available, running well in GPRS or in WiFi network, thanks, the problem has been resolved, and the next step is to analyze the code.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice