Android Tutorial [B4X] MQTT Chat Room

Status
Not open for further replies.

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

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.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…