B4J Question Irregular characters when sending via MQTT

warayTek

Member
Licensed User
Hi everyone, I am trying to test the MQTT chat app. When I try to send a string I have the following output.
Using B4J
demoapp.png

B4X:
Public Sub SendMessage(Body As String)
    If connected Then
        Log(Body)
        client.Publish2("home/light/livingRm", CreateMessage(Body), 0, False)
    End If
End Sub
Output from Ubuntu Terminal(broker side)
1710818818815.png

But when using MQTT explorer app, it sends okay.

Thanks for any input.
 

DonManfred

Expert
Licensed User
Longtime User
Are you sending serialized data? What does createMessage do?

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

if you are using this example

Note that it should work in all B4X projects.

The serialized data are not meant to print in serverside...
 
Last edited:
Upvote 0

warayTek

Member
Licensed User
Are you sending serialized data? What does createMessage do?

Note


if you are using this example

Note that it should work in all B4X projects.

The serialized data are not meant to print in serverside...
Thanks for the time. I got it working.
 
Upvote 0

warayTek

Member
Licensed User
Temporarily commented this line.
"serializator.ConvertBytesToObject()"

And to send a test message use the
client.Publish("home/light/livingRm","Hello from B4J".GetBytes("UTF8"))

I have another question, how to receive the message in B4J?
This the command I use in the terminal (broker side) mosquitto_pub -h 192.168.1.44 -t home/light/livingRm -m "Hello earthling."
 
Upvote 0

warayTek

Member
Licensed User
run a MQTT-Client in B4J and subscribe to home/light/livingRm

BTW: The B4J-client is able to work work the serialized-data.
"BTW: The B4J-client is able to work work the serialized-data." okay I will try it.

it gives an error when it receives the message.
B4X:
Private Sub client_MessageArrived (Topic As String,Payload As String)
Log(Topic)
End Sub
here is the error
B4X:
Connected: true
1
java.lang.Exception: Sub client_messagearrived signature does not match expected signature.
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
    at anywheresoftware.b4a.BA$3.run(BA.java:267)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
it gives an error when it receives the message.
check the b4j-client-example

this is how the code should look like

B4X:
Private Sub client_MessageArrived (Topic As String, Payload() As Byte)
    Dim receivedObject As Object = serializator.ConvertBytesToObject(Payload)
    If Topic = "all/connect" Or Topic = "all/disconnect" Then
        'new client has connected or disconnected
        'do nothing. The server will handle it.
    Else if Topic = "all/users" Then
        Dim newUsers As List = receivedObject
        Main.NewUsers(newUsers)
    Else
        Dim m As Message = receivedObject
        Main.NewMessage(m)
    End If
       
End Sub

This expects to get the payload as serialized though ;-)
 
Last edited:
Upvote 0
Top