Bridge MQTT Connection between Servers with username - mqtt

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.

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

is it possible to use two mqtt listening ports with single broker

we are using emqx (3.0). my emqx is running in 1883 port and it's getting data from devices with authentication(device authentication and topic authorization) in same port. I want to enable one more port for device un-authentication. That's why I enabled one more port for the same emqx. now emqx is running in two ports with same process id. I checked with netstat command it's showing same process id for both ports(1883 & xxxx). but through paho I am unanle to connect to emqx from xxxx port. it's showing connection lost error.
1883 port is working fine.
in dashboard also xxxx port is enabled.
but I'm unable to connect through paho.
Multiple listeners with different configurations are supported by setting another zone in the emqx.conf. The default zones are external and internal:
listener.tcp.external = 0.0.0.0:1883
listener.tcp.external.acceptors = 8
listener.tcp.external.zone = external
...
listener.tcp.internal = 127.0.0.1:11883
listener.tcp.internal.acceptors = 4
listener.tcp.internal.zone = internal
You can add your own ones or just change the internal one as follows, if you don't need the internal zone:
listener.tcp.internal = 127.0.0.1:xxxx
listener.tcp.internal.acceptors = 8
listener.tcp.internal.zone = external
...

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")

Remote connection to Neo4j server

I believe the way to creating a remote connection is by changing this line in conf/neo4j-server.properties, specifically by removing the comment and restarting the server.
org.neo4j.server.webserver.address=0.0.0.0
My URL is https://0.0.0.0:7473/browser/ and works on the local machine, but when I test the URL in Safari on iPhone over 3G, it cannot connect.
What do I set the address to in the properties file?
I thought it was the IP address of my computer, but after trying the remote address which I got from Googling “ip address mac” that didn’t work, nor did (obviously) the local IP address of my machine, 192.168.0.14
I should point out that setting it to the IP address from Google throws an error and the log reads:
2015-01-29 17:10:08.888+0000 INFO [API] Failed to start Neo Server on port [7474], reason [MultiException[java.net.BindException: Can't assign requested address, java.net.BindException: Can't assign requested address]]
With default configuration Neo4j only accepts local connections
In neo4j-community-3.1.0 edit conf/neo4j.conf file and uncomment the following to accept non-local connections
dbms.connectors.default_listen_address=0.0.0.0
By setting
org.neo4j.server.webserver.address=0.0.0.0
enables Neo4j on all network interfaces.
The remainder of that reply is not Neo4j related at all - it's regular networking. Double check if port 7473 (and/or 7474) are not blocked neither be a locally running firewall nor by your router. You local IP 192.168.0.14 indicates you're behind a router doing NAT. Therefore you have to setup a port forwarding in your router for the ports mentioned above.
Please be aware that this is potentially dangerous since everyone knowing your external IP can access your Neo4j instance. Consider using either https://github.com/neo4j-contrib/authentication-extension or use a VPN in favour of port forwarding.
in 3.0:
##### To have HTTP accept non-local connections, uncomment this line
dbms.connector.http.address=0.0.0.0:7474
Confused myself with the setting. Anyone who has the same problem, 0.0.0.0 just means “this server isn’t local any more” and so to access it you use the public IP address of the computer that’s hosting the Neo4j server.
Just make sure that the ports you set in the server properties (default are 7474 and 7473) are open for incoming connections on your router/firewall etc.
I think there's some confusion here. That configuration property org.neo4j.server.webserver.address is about which IP address the server you're starting listens on for external connections. Relevant documentation is here.
It seems you're asking how to configure your database to talk to a remote database. I don't think you can do that. Rather, by editing that file you're planning on running a database on the host where that file is. Your local database on that host will write files to wherever the org.neo4j.server.database.location configuration parameter points.
A remote connection is something that the neo4j shell might establish, or that you browser might make to a foreign server running neo4j; but you don't establish that sort of remote connection by editing that file. Hopefully this helps.
Also if you have ssh access to remote server with neo4j you can setup ssh tunnel to access it via localhost:
ssh -NfL localhost:7474:localhost:7474 -L localhost:7687:localhost:7687 yourname#yourhost
then type in browser:
localhost:7474
Depends on the version.
Look for the phrase 'non-local connections' in the conf file.(In my case, $NEO4J_HOME/conf/neo4j.conf)
Then follow the instructions in the comments.
In my case,
# With default configuration Neo4j only accepts local connections.
# To accept non-local connections, uncomment this line:
server.default_listen_address=0.0.0.0

Resources