MQTT

Erel

B4X founder
Staff member
Licensed User
Longtime 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: https://www.b4x.com/android/forum/pages/results/?query=mqtt
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:
B4X:
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
     DrawCircleData(cd)
   Else 'obj is string
     Dim s As String = obj
     Select s
       Case "clear"
         Canvas1.ClearRect(0, 0, Canvas1.Width, Canvas1.Height)
       Case "close"
         MainForm.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)
   DrawCircleData(cd)
   mqtt.Publish(mytopic, serializator.ConvertObjectToBytes(cd))
End Sub
 

woniol

Active Member
Licensed User
Longtime User
That is great news to have official MQTT library for B4J and B4A.
What mqtt broker was used in this demo?
 

Beja

Expert
Licensed User
Longtime User
As always, Erel,

you are great.......... Thanks a million
 

LucaMs

Expert
Licensed User
Longtime 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:

LucaMs

Expert
Licensed User
Longtime 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"?
 

b4auser1

Well-Known Member
Licensed User
Longtime User
Erel,

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.
 

b4auser1

Well-Known Member
Licensed User
Longtime 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.
 

LucaMs

Expert
Licensed User
Longtime 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
 

b4auser1

Well-Known Member
Licensed User
Longtime User
CloudMQTT support MQTT over websockets. MQTT over websockets allowes every browser to be a MQTT client. You can push messages to your browser when an event occurs or publish messages to your device. CloudMQTT Websockets Port (TLS only) can be found on the details page for your instance.

The Paho JavaScript Client is a browser-based library that can be used when connecting WebSockets to an MQTT server. Reference documentation can be found here.

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 https://www.cloudmqtt.com/docs.html

P.S. I am really interested more in the security settings
how to use ClouDMQTT (SSL, authentication and authorization)
, but I believe many people are interested in WebSockets as well.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
1. Download: www.b4x.com/b4j/files/MQTTCanvas.jar
2. Double click on the jar. You should see this window:

SS-2015-10-19_11.54.32.png


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:

coslad

Well-Known Member
Licensed User
Longtime User
Hi Erel, if your post is the answer to me, i meant to create the server broker on Android. If not, excuse me
 

Mark Turney

Active Member
Licensed User
Longtime 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 :)!
 

cxdzbl

Active Member
Licensed User
Try this:
1. Download: www.b4x.com/b4j/files/MQTTCanvas.jar
2. Double click on the jar. You should see this window:

SS-2015-10-19_11.54.32.png


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.


error connecting

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

Attachments

  • 360截图20170516112355734.jpg
    360截图20170516112355734.jpg
    14.1 KB · Views: 454
  • 360截图ccc.jpg
    360截图ccc.jpg
    12.3 KB · Views: 506
Last edited:
Top