B4J Question MQTT Connection & messages

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am beginning to learn MQTT as Erel recommend I look into it from one of my other threads.

I have looked at Erel's MqttBroker Lib - https://www.b4x.com/android/forum/threads/mqttbroker.61548/#content

So far it looks simple to use.

Using the following code:

B4X:
Sub Process_Globals
   Dim broker As MqttBroker
End Sub

Sub AppStart (Args() As String)
   broker.Initialize("", 51041) 'first parameter is the event name. It is currently not used.
   broker.Start
   StartMessageLoop 'Non-UI app
End Sub

It has allowed me to run the broker within B4J and allow the B4J/B4A/B4i Client to connect fine.

Based on my example below, DeviceA, DeviceB & DeviceC are devices out in the field and I can't modify it. They only communicate using TCP and send ASCII messages to a IP/Port. I have been able to get them to communicate to my B4J app fine so far.

Phone1, Phone2 & Phone3 I do have control on how messages are sent and received.

So I plan on using MQTT to send messages from the phone to the B4J server.

Each phone should not send messages to each other and should only talk to the B4J app only.

For Example:
Phone1 & Phone3 will be able to send a message to the B4J server and then it forward the message to the device.

Phone1 > DeviceA Only.
Phone3 > DeviceA Only.

However Phone1 & Phone3 should not see each others messages.

So for an example, phone 1 sends the message to ask DeviceA what the time is, but Phone 3 doesn't care that phone 1 asked what the time is so this message shouldn't be sent to phone 3.

upload_2016-7-5_11-11-17.png


Any ideas on how make it so that each of the phones don't send the message to other phones and only send it to the B4J app ?

As I am still learning about MQTT, from what I have seen MQTT is a subscription message service so if you are subscribed to that topic, if any messages are sent to that topic then everyone who is subscribe to that topic will receive the message, and you can't limit who can/can't receive the message ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Any ideas on how make it so that each of the phones don't send the message to other phones and only send it to the B4J app ?
The server can subscribe to "B4J". Other devices will send messages with this topic. The messages will only reach the server.

You can let each device subscribe to a unique topic and then send messages to the specific devices based on the topics.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
The server can subscribe to "B4J". Other devices will send messages with this topic. The messages will only reach the server.

You can let each device subscribe to a unique topic and then send messages to the specific devices based on the topics.
Well that was easy :)
 
Upvote 0
Top