B4J Library MqttBroker

Status
Not open for further replies.

inakigarm

Well-Known Member
Licensed User
Longtime User
I've an application prototype that uses B4J Mqtt Broker and I'd want to implement a Webapp server to catch all the messages events ,display them on a console window and save on a MYSQL DB.

Looking at Mqtt broker object, I can't access the events from code (don't expose the on connect, publish or disconnect events), so :

-Is there anyway to access this events from code ?? (I know I can save the log to a file -tslogger/jlogger-, but it should be simple if the events were accessible)

-Is there anyway to read the payload messages from mqtt broker to save to an external DB?

(Log of B4J MQTT Broker on UI app)
B4X:
Received a message of type CONNECT
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping CONNECT
cleaning old saved subscriptions for client <User1>
Create persistent session for clientID <User1>
Connected client ID <User1> with clean session true
Received a message of type SUBSCRIBE
Received a message of type SUBSCRIBE
Received a message of type SUBSCRIBE
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping SUBSCRIBE
<User1> subscribed to topic <drawers/#> with QoS 0 - MOST_ONE
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping SUBSCRIBE
<User1> subscribed to topic <last> with QoS 1 - LEAST_ONE
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping SUBSCRIBE
<User1> subscribed to topic <user1> with QoS 1 - LEAST_ONE
Received a message of type PINGREQ
Received a message of type CONNECT
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping CONNECT
cleaning old saved subscriptions for client <User2>
Create persistent session for clientID <User2>
Connected client ID <User2> with clean session true
Received a message of type SUBSCRIBE
Received a message of type SUBSCRIBE
Received a message of type SUBSCRIBE
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping SUBSCRIBE
<User2> subscribed to topic <drawers/#> with QoS 0 - MOST_ONE
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping SUBSCRIBE
<User2> subscribed to topic <last> with QoS 1 - LEAST_ONE
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping SUBSCRIBE
<User2> subscribed to topic <user2> with QoS 1 - LEAST_ONE
Received a message of type PINGREQ
 

jayel

Active Member
Licensed User
Longtime User
Hey Erel,

Is there a way I can connect to the broker with pure javascript?
B4J apps I have no problem but I want to change my php site to have a page that can subscribe to my broker (B4J mqtt broker)

John
 

jayel

Active Member
Licensed User
Longtime User
What do you mean? I use websockets for my connection from my php page with javascript to my B4J broker?
I tried with this (https://eclipse.org/paho/clients/js/), but it gives an error :
WebSocket connection to 'ws://192.168.xxx.xxx:51041/mqtt' failed: Connection closed before receiving a handshake response

B4X:
<script type="text/javascript">

// Create a client instance
var client = new Paho.MQTT.Client("192.168.xxx.xxx", 51041, "clientId_" + parseInt(Math.random() * 100, 10));

console.log(client);
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({onSuccess:onConnect});


// called when the client connects
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("all/#");
  message = new Paho.MQTT.Message("Hello");
  message.destinationName = "all/connect";
  client.send(message);
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:"+responseObject.errorMessage);
  }
}

// called when a message arrives
function onMessageArrived(message) {
  console.log("onMessageArrived:"+message.payloadString);
}
</script>


Any ideas?
 

jayel

Active Member
Licensed User
Longtime User
So, B4J webserver with my B4J broker and than websockets that connects my php page with the data received fom the broker aka my B4J webserver?
 

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi

Does mqttbroker has Event ? What are they ?
 

janderkan

Well-Known Member
Licensed User
Longtime User
Hi

I created an Android app using jMqttBroker (version: 1.03).

Dim broker As MqttBroker

broker.Initialize("", 51041)
broker.Start

It works FINE in debug mode,

LOG debug mode:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Persistent store file: //moquette_store.mapdb
Starting without ACL definition
Server binded host: 0.0.0.0, port: 51041
Server binded host: 0.0.0.0, port: 8080
** Activity (main) Resume **


but when I try to run in release mode I get a lot of errors,

LOG release mode:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Persistent store file: //moquette_store.mapdb
Starting without ACL definition
main_activity_create (java line: 327)
io.netty.channel.ChannelException: Unable to create Channel from class class io.netty.channel.socket.nio.NioServerSocketChannel
at io.netty.bootstrap.AbstractBootstrap$BootstrapChannelFactory.newChannel(AbstractBootstrap.java:457)
at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:308)
at io.netty.bootstrap.AbstractBootstrap.doBind(AbstractBootstrap.java:273)
at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:269)
at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:251)
at org.eclipse.moquette.server.netty.NettyAcceptor.initFactory(NettyAcceptor.java:142)
at org.eclipse.moquette.server.netty.NettyAcceptor.initializePlainTCPTransport(NettyAcceptor.java:155)
at org.eclipse.moquette.server.netty.NettyAcceptor.initialize(NettyAcceptor.java:105)
at org.eclipse.moquette.server.Server.startServer(Server.java:96)
at anywheresoftware.b4j.objects.MqttBroker.Start(MqttBroker.java:72)
at b4a.example.main._activity_create(main.java:327)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
at dalvik.system.NativeStart.main(Native Method)
Caused by: io.netty.channel.ChannelException: Failed to open a server socket.



I tried the Android SimpleChat app, it Works fine.
I have included the same libraries.
 
Last edited:

Hypnos

Active Member
Licensed User
Longtime User
Just tried this mqtt borker, when the service start, I can see the server also binded to port 8080, what is the usage for this port? When I use my browser go to this port 8080, I got the following message:

"not a WebSocket handshake request: missing upgrade"

What's this mean ?

Thanks!
 

uniplan

Active Member
Licensed User
Longtime User
So, B4J webserver with my B4J broker and than websockets that connects my php page with the data received fom the broker aka my B4J webserver?

Also I would like to achieve something similar. You've made a few examples that can be of help?
Thank you.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…