How paho client can know status of bridge connections? - mqtt

I have one remote broker (cloudmqtt) and one local broker on my board. Both are connected as bridge. I have one paho client connected to local broker. I want to know the status of bridge in order to publish message. I know I can publish message to local broker without knowing status and broker will take care. But I want to design my application like I will Publish message only if bridge is Up.
I am using paho client library in C and mosquitto broker v1.6.
Below is my conifg file :
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
log_timestamp true
log_timestamp_format %Y-%m-%dT%H:%M:%S
log_type all
user root
connection cloudmqtt
address xxxx.cloudmqtt.com:13287
remote_username xxxxxxx
start_type automatic
try_private true
remote_password xxxxxx
notifications true
notification_topic /broker/connection/state
restart_timeout 20
max_queued_messages 0
topic # both 2

The short answer is you can't at a pure MQTT protocol level or specific to the Paho client (and you shouldn't care for the reasons you mentioned).
Now having said all that you can actually get messages about the bridge status from the $SYS/broker/connection/<remote-clientid>/# topic tree on mosquitto. To enable this you need to set the notification true flag in the bridge config. The doc for configuring bridges is here

Related

Is it possible to bridge MQTT broker mosquitto to Cloud PubSub?

I have a local mosquitto broker that I would like to connect to Google Cloud pubsub queue.
I looked into Google Cloud IOT core but with no luck, I cannot seem to connect to it
Here is my mosquitto configuration
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
# Goolge IoT Core Configuration
connection bridge-to-gcp
address mqtt.googleapis.com:8883
bridge_attempt_unsubscribe false
bridge_certfile /Downloads/rsa_cert.pem
bridge_keyfile /Downloads/rsa_private.pem
bridge_cafile /Downloads/roots.pem
bridge_protocol_version mqttv311
bridge_insecure false
tls_version tlsv1.2
try_private true
start_type automatic
cleansession true
notifications false
local_clientid local-to-remote-gcp-bridge
remote_clientid projects/toolsense-dev/locations/europe-west1/registries/test-registry/devices/test-device
topic # both
Is there a way to forward all the messages received from edge devices to Google Cloud Pubsub?
After deeper research, it is not possible without the existence of a gateway device either virtually or physically. It could even be client code running on same machine as the one running the broker.
This usecase is however possible in AWS Iot
https://wiki.seeedstudio.com/Arduino-AWS-IOT-Bridge/

"Invalid bridge configuration" when set "notifications : true"

I want to get event of connection and disconnection of every mqtt client.
Here is my config file
Is any way to achieve this ?
notifications true
listener 1883 192.168.22.54
protocol mqtt
listener 11883 192.168.22.54
protocol websockets
The notifications configuration option is not for tracking normal client connections. It is only for use as part of a bridge configuration and the notifications are only about the state of the bridge.
As you have added it to the config file but no other details about a bridge then the broker is rightly complaining that it is not part of a valid bridge configuration.

Bridge MQTT Connection between Servers with username

I'm trying to use mosquitto to bridge 2 other mqtt servers.
EXTERNAL > MOSQUITTO > INTERNAL
Internal requires a Username to be set. Is it possible to forward the username that is used to connect to MOSQUITTO to be forwarded to INTERNAL as well?
# connection internal
connection internal
address internal:1883
topic # out 0
topic # in 0
No, bridges are a one time thing, you do not get a bridge per connected user.
You can specify a username/password for (each end) of a bridge connection using the remote_username, remote_password, local_username and local_password config options as described in the mosquitto.conf man page.
The local_ prefix are useful to ensure that ACLs are followed for connections and when allow_anonymous false is set.

Connect local Mosquitto MQTT broker to Google Cloud IoT

In my current setup i read data using ebusd from my heating system which works perfectly fine. In the next step i'd like to send this data to a Google IoT Core Device. Unfortunately ebusd does not support setting the clientid externally.
To circumvent this i installed a local Mosquitto Broker which accepts unsecured messages in my Raspberry Pi and shall bridge them to the Google IoT Core Broker. Here is how i configured my local Mosquitto Broker:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_type all
log_dest file /var/log/mosquitto/mosquitto.log
# Goolge IoT Core Configuration
connection bridge-to-gcp
address mqtt.googleapis.com:8883
bridge_attempt_unsubscribe false
bridge_certfile /home/pi/certificates/rsa_cert.pem
bridge_keyfile /home/pi/certificates/rsa_private.pem
bridge_cafile /home/pi/certificates/roots.pem
bridge_protocol_version mqttv311
bridge_insecure false
tls_version tlsv1.2
try_private true
start_type automatic
cleansession true
notifications false
local_clientid local-to-remote-gcp-bridge
remote_username unused
remote_password <JWT Token generated using jwt.io>
remote_clientid projects/project-251313/locations/europe-west1/registries/prot/devices/ebusd12345
topic # both
include_dir /etc/mosquitto/conf.d
Although this setup works with AWS IoT with Google Cloud IoT i get
1542622526: Connecting bridge bridge-to-gcp (mqtt.googleapis.com:8883)
1542622526: Bridge projects/project-251313/locations/europe-west1/registries/prot/devices/ebusd12345 sending CONNECT
1542622526: Socket error on client local.projects/project-251313/locations/europe-west1/registries/prot/devices/ebusd12345, disconnecting.
After googling some time i found this blog (see http://community.onion.io/topic/2858/omega-onion-connecting-to-cloud-cloud-iot/2) which states that Google Cloud IoT Core does not support connections by "sub-brokers" but i didn't find a second statement claiming the same.
Can anybody either confirm that i am trying to do something impossible or please help with with configuring the broker properly?
I reached out to the author of that blog and other people who work on Cloud IoT Core and unfortunately it seems like MQTT bridging indeed is currently not supported. In the meantime, it seems like you're in contact with the ebusd owner on Github to add a configurable clientid feature, which would be a good temporary fix.
With that said, we have a feature that will be going into beta soon that will help you address this kind of issue specifically, so stay tuned!

Why when cleansession is enabled and set to true in Mosquitto.Conf Mosquitto Broker refuses to start?

I recently upgraded and updated my Mosquitto Broker to 1.4.15 on Raspberry-pi3. It works as expected. However, every time I enable and set cleansession to true in the Mosquitto.Conf file, Mosquitto broker refuses to start. As soon as I comment out or disable cleansession, Mosquitto broker starts up immediately by its service. The reason I need to set this to true is because I want the Mosquitto Broker to clean up any disconnected clients' session. So that the same client can reconnect again to the broker. Am I doing this right? or Is there another Mosquitto Broker's feature that I can use in place of cleansession?
The cleansession flag in the mosquitto.conf is to control what options mosquitto uses when it connects to remote brokers as a client when setting up a bridge.
If you want your clients to connect with a clean session then you need to set that option in your client library when setting up the connection not on the broker.

Resources