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

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.

Related

Can I create a listener in mosquitto, bound to a device which may not be present?

I am running mosquitto on a raspberry pi and it all functions fine with an ethernet connection. I have two listeners, one limited to localhost for most of the functionality and another limited to the vpn ip address so that I can do some administration over vpn, all others are then excluded. The problem is that once this device goes out into the world it may or may not have an internet connection, but if the internet connection is not there, the vpn address does not exist and mosquitto fails to start.
I am using mosquitto v2.0.4, but I can update if required.
Does anyone have any suggestions on how I can solve this?
per_listener_settings true
retain_available true
log_dest stdout
listener 1883 localhost
allow_anonymous true
listener 1886 10.8.0.100
max_connections 1
allow_anonymous true
No, if you specify a listener for a non existent IP address then mosquitto will fail to start.
The solution is probably to specify a listener without an IP address (so it will bind to 0.0.0.0 and be available on all listeners) and rely on suitable authentication to limit access rather than the connection arriving on a specific interface.
If really needed you can use the firewall on the device to filter connections to that port to only be allowed from a specific subnet that matches that configured for the VPN.

How paho client can know status of bridge connections?

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

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.

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.

Setting up Paho Javascript Client (MQTT) with mosquitto on AWS EC2 Ubuntu + Ruby on Rails

I have been trying to setup a MQTT broker on my AWS EC2 server, using the port 1883. So far it works with the ruby-mqtt gem, but I am having trouble with setting it up with Paho Javascript Client for the website.
What I have done so far:
Mosquitto
Installed mosquitto on my AWS EC2 instance, and it is running and listening on port 1883. I subscribed to a topic locally using the command
mosquitto_sub -h localhost -p 1883 -v -t 'topic1'
AWS EC2 Security Group
Allow traffic over port 1883 (under tcp protocol)
Ruby on Rails
Installed ruby-mqtt gem, and tested the mqtt to be working by running the code below in rails console (development environment)
MQTT::Client.connect(ip_address_or_domain_name) do |c|
c.publish('topic1', 'message to topic 1')
end
The message appears in the terminal where mosquitto_sub was running.
Nginx
All this was done without any configuration on Nginx configuration files.
Paho Client
So I fired up a local rails server on my local computer, and run the example javascript snippet on one of my html view.
// Create a client instance
client = new Paho.MQTT.Client("mqtt.hostname.com", Number(1883), "", "clientId")
// 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("topic1");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "topic1";
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);
}
But I fail to connect. The error I am getting in the chrome developer console is:
WebSocket connection to 'ws://mqtt.example.com:1883/' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
Im not sure what is wrong here. Greatly appreciate any help! Thanks in advance!
So the problem is Paho Javascript Client states that the parameter for client object has to be
the address of the messaging server, as a fully qualified WebSocket URI, as a DNS name or dotted decimal IP address.
So making it listen to port 1883, which is the standard port for mqtt, will not work.
ruby-mqtt works as it is because it parameter is treated as a mqtt uri
In other words, Paho connects via ws://host while ruby-mqtt connects via mqtt://host. The latter connects to port 1883 with the correct protocol (not sure if this is the right word here) for the
correct port.
So Paho has to connect to another port where the websocket protocol can be used.
This is my solution.
Mosquitto
Version need to be at least 1.4 where websocket is supported. I add the last 3 lines to the default mosquitto.conf file.
# /etc/mosquitto/mosquitto.conf
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
port 1883
listener 1884
protocol websockets
This opens 2 ports for mosquitto to subscribe to over 2 different protocols respectively.
AWS Security Group
Allow traffic over port 1884 (under tcp protocol)
Paho Client
mqtt.hostname.com
change just the line where the client object is initialized to
client = new Paho.MQTT.Client("mqtt.hostname.com", Number(1884), "", "clientId")

Resources