Android Tutorial [B4X] MQTT Chat Room

Status
Not open for further replies.
It's time to learn how to use MQTT: https://www.b4x.com/android/forum/threads/59471/#content
It is simple and powerful. In most cases MQTT is the best solution for low level networks. Low level network means a network solution that is traditionally based on ServerSocket + Socket + AsyncStreams.

In this example we implement a chat room with one or more users. The MQTT broker is embedded in the Android app (jMqttBroker library). The clients are Android, iOS or desktop apps. Any number of users can join.


upload_2015-12-21_14-42-4.png
upload_2015-12-21_14-43-50.png


SS-2015-12-21_14.40.07.png


When you start the Android app you can choose whether it will be the server. The other clients will connect to the server.

The messages are serialized with B4XSerializator (RandomAccessFile library). B4XSerializator is very useful for cross platform communication.

Users can join and leave the chat room. The list of users will be updated automatically.
Note the usage of the LastWill feature. When a client is disconnected unexpectedly (and only if it is unexpectedly) the LastWill message will be sent. This allows us to remove the client from the list.

The UI state is managed by StateManager (in B4A and B4i).
It is a simple and flexible UI implemented with anchors. Note that it properly handles the soft keyboard changes.

jMQTT library: https://www.b4x.com/android/forum/threads/59472/#content
jMqttBroker: https://www.b4x.com/android/forum/threads/mqttbroker.61548

Extension to this example with auto discovery: https://www.b4x.com/android/forum/posts/480542/
 

Attachments

  • Chat_B4i.zip
    6.1 KB · Views: 2,130
  • Chat_B4J.zip
    3.8 KB · Views: 3,401
  • Chat_B4A.zip
    11.3 KB · Views: 2,579
Last edited:

deantangNYP

Active Member
Licensed User
Longtime User
Hi, May i know if its possible to Publish in string format? How to change the following Publish2 format?
--> something like this: client.Publish2("all/connect", edtText.text, 0, False)

The following code is from the SimpleChat program which i would like to reuse. Thanks
B4X:
client.Subscribe("all/#", 0)
client.Publish2("all/connect", serializator.ConvertObjectToBytes(currentName), 0, False)
 

woniol

Active Member
Licensed User
Longtime User
Hi,
if currentName is defined as String this will work
B4X:
client.Publish2("all/connect", currentName.getBytes("UTF8"), 0, False)
 

ValhallSW

Member
Licensed User
Longtime User
In this example (top post), the Android device is setup as Server. How can I setup the Computer (B4J) as server. That is the one device that does not change the IP (at least in my house)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Running a broker requires three lines of code:
B4X:
Dim broker As MqttBroker 'process global variable
broker.Initialize("", <port>)
broker.Start
Move this code to B4J and the broker will run on the B4J.

You will also need to move this code:
B4X:
If isServer Then
   Log($"${Topic}: ${newUser}"$)
   Dim index As Int = users.IndexOf(newUser)
   If Topic.EndsWith("connect") And index = -1 Then users.Add(newUser)
   If Topic.EndsWith("disconnect") And index >= 0 Then users.RemoveAt(index)
   client.Publish2("all/users", serializator.ConvertObjectToBytes(users), 0, False)
End If
 

coslad

Well-Known Member
Licensed User
Longtime User
Hi

i tried the example , installing the app on 3 phones , but the message is added into the received list only if the screen is on , and if i turn off the wifi on a client its name is alway present into the user connected list !
 

coslad

Well-Known Member
Licensed User
Longtime User
i solved the first issue , i changed CallSub2 to CallSubDelayed2 ,the second issue is maybe due to the server alive timeout ,but it is too long .

Also if the server crashes , no client aware it ,and it seems everything works fine but not !
 

aidymp

Well-Known Member
Licensed User
Longtime User
Hi, I have a VPS and installed mosquito all seems to be working fine, however using this chat example, the Java version works fine except i see no list of users, but i get messages, and can send them too, on the Android version I enter the IP of my server, click client, and I get a log message saying connected but nothing else happens?? i dont get the chat window up. I have changed the port in both apps, to 1883 and i have tried a few things on the server and they all seem to work fine? Any clues as to what the problem could be?

B4J connects can send and receive messages, dont see any list of users
B4A Says connected in the log, but program stays on the connect screen, dont see any messages in the log.

I haven't tried with any other broker even the local (android one) as I need this to work online.

Thanks

Aidy
 

aidymp

Well-Known Member
Licensed User
Longtime User
Start with the example exactly as it is, with the broker running on the Android. In this example the Android is the server. You will need to modify the code if you use an external broker.

As always the voice of reason! lol I will do that Erel, but what would the changes be? just the port and url or more?

thanks

Aidy
 

Gandalf

Member
Licensed User
Longtime User
Running a broker requires three lines of code:...
Hi Erel,

At first, I would like to say thank you for this great project. It really helps me with both of my jobs.

Now I have a problem running MQTT broker on PC with B4J and connecting client to it in the same application. I did exactly what you recommended in post 420345 but it did not work. I started from blank but still I have the same problem - client does not connect. I have disabled Windows firewall just in case - no success. What confuses me is zeros in "Server binded host: 0.0.0.0, port: 51042" broker debug info. I doubt it should look like this. I run it on Win10, can this be the cause? I'm sure that my mistake is obvious and stupid :) Source code is attached.
 

Attachments

  • Side0.zip
    27.5 KB · Views: 514

Beja

Expert
Licensed User
Longtime User
Thanks Erel
I know iPhone is too arrogant to say anything.. Just dots :)
 

ValhallSW

Member
Licensed User
Longtime User
I have now tried different ways to change the code of the Chat_B4J,
but I still cannot get the computer/B4J to be the broker/server instead of one of the mobile devices.
Any help is appreciated. o_O
 
Last edited:
Status
Not open for further replies.
Top