Mosquitto: published mqtt message not delivered to websockets client - websocket

I am running mosquitto on Windows, and configured it to work as mqtt on port 1883, and websockets on port 9001. I thought I could publish a message from an mqtt client and receive it on a websockets client, but it appears not to be so.
I can successfully deliver a message from an mqtt client to another mqtt client, as well as from a websockets client to another websockets client. All in all, there seems to be a impassable boundary between the two protocols.
Is this a limitation of Mosquitto?
Have I forgotten to set something in the conf file?
Note: I invoke Mosquitto with a -v option, so it logs verbosely. I have noticed that it actually logs connections and messages for websockets clients, but not for mqtt clients, although on mqtt client side everyting regarding connection and publishing seems ok (I am using MQTTNet client to publish, either in mqtt or in websockets mode, and PAHO javascript client, in websockets mode, for subscribing/receiving).
Disabling or enabling the firewall didn't make any difference.
Edit:
Here is the relevant section on the conf file:
listener 1883
protocol mqtt
listener 9001
protocol websockets
password_file mypasswordfile.pwd
This is what it reports when I launch it:
prompt> ./mosquitto -c myconfig.conf -v
2022-10-15 20:25:53: mosquitto version 2.0.15 starting
2022-10-15 20:25:53: Config loaded from myconfig.conf.
2022-10-15 20:25:53: Opening ipv6 listen socket on port 1883.
2022-10-15 20:25:53: Opening ipv4 listen socket on port 1883.
2022-10-15 20:25:53: Opening websockets listen socket on port 9001.
2022-10-15 20:25:53: mosquitto version 2.0.15 running
This is the relevant code fragment from the publisher (but I have also tried the mosquitto_pub utility):
var mqttFactory = new MqttFactory();
var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder().
// WithTcpServer("localhost"). // This does NOT work
WithWebSocketServer("ws://localhost:9001/mqtt"). // This works
WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311).
WithClientId("publisher-client-id").
WithCredentials("myusername", "mypassword").
Build();
mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None).Wait();
var builder = new MqttApplicationMessageBuilder();
var applicationMessage = builder
.WithTopic("mytopic")
.WithPayload("mypayload")
.Build();
mqttClient.PublishAsync(applicationMessage, CancellationToken.None).Wait();
and here is a code fragment from the subscriber:
mqttConnect() {
this._mqtt = new Paho.MQTT.Client('localhost', 9001, '/mqtt', 'subscriber-client-id');
this._mqtt.onConnectionLost = this.onMqttConnectionLost.bind(this);
this._mqtt.onMessageArrived = this.onMqttMessageReceived.bind(this);
const options = {
mqttVersion: 4,
userName: 'myusername',
password: 'mypassword',
onSuccess: this.onMqttConnected.bind(this),
onFailure: this.onMqttFailedConnecting.bind(this)
};
this._mqtt.connect(options);
...
}
onMqttConnected() {
console.log('Connected to MQTT broker');
...
this._mqtt.subscribe('mytopic');
}
onMqttMessageReceived(message) {
console.log('Received MQTT message; topic = ' + message.destinationName + ', payload = ' + message.payloadString);
...
}

As thrashed out in the comments, there were 2 instance running (one the Windows service, one in the command prompt).
Shutting down the background service fixed the problem.
(It is unclear why both instances were able to bind to port 1883)

Related

How to publish data(Uplink Measures) supports MQTT protocol with Mosquitto publisher client to The Things Network(TTN) as broker

Problem: I am unable to publish uplink measures to TTN(The Things Network,MQTT Broker) through MQTT Publisher Client. Follow these steps...
Installed Eclipse Mosquitto providers a CLI to subscribe and to publish
messages.
Start Mosquitto Service.
start mosquitto service
MQTT command to publish uplink measure:
mosquitto_pub -h eu.thethings.network -p 1883 -u applicationid-P ttn-account-v2.xXXXXXXXXXXXXXXXXXXXXXXXXX -t applicationid/devices/deviceid/up -m '{"port":1,"payload_raw":"AWcAuw=="}' -d
MQTT command to subscribe uplink measure:
mosquitto_sub -h eu.thethings.network -p 1883 -u applicationid -P ttn-account-v2.x2Q20I2hDo1XXXXXXXXXXXXXXXXX -t applicationid/devices/deviceid/up
Double check that you can really reach to this 52.169.76.255 host and 1883 port using telnet tool or equivalent, for example I cannot connect to this server.
According to WHOIS information the IP address belongs to Microsoft corporation so my expectation is that you're trying to test an application which is deployed in Azure cloud infrastructure. I would recommend checking if port 1883 is open for incoming connections and if not you will need to configure the VM and open the port (you might also need to do the same in the OS firewall)
Once you will be able to establish the connection using telnet (or equivalent) JMeter should also be able to connect and send/receive the messages.
Just in case check out Testing the MQTT Messaging Broker for IoT - A Guide

Mosquitto ERR_CONNECTION_REFUSED using websockets (paho client) on win 10

I've read all the threads with similar questions, but couldn't find an answer.
Mosquitto config:
listener 1883 127.0.0.1
protocol mqtt
listener 9001 127.0.0.1
protocol websockets
log output:
1567705166: mosquitto version 1.6.2 starting
1567705166: Config loaded from C:\Program Files (x86)\mosquitto\mosquitto.conf.
1567705166: Opening ipv4 listen socket on port 1883.
1567705166: Opening websockets listen socket on port 9001.
1567705166: Opening websockets listen socket on port 1883.
Chrome devtools:
mqttws31.js:977 WebSocket connection to 'ws://127.0.0.1:9001/mqtt' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I've tried many things but nothing helped:
Trying websockets only
Trying another port (1883 and 9001 instead of 8080)
Switching off Windows firewall
If I change the config file to:
#listener 1884 127.0.0.1
#protocol mqtt
#listener 1883 127.0.0.1
protocol websockets
Mosquitto listens for websockets on port 1883 but logfile reads:
1567706943: mosquitto version 1.6.2 starting
1567706943: Config loaded from C:\Program Files (x86)\mosquitto\mosquitto.conf.
1567706943: Opening websockets listen socket on port 1883.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
1567706943: Error in poll: No error.
changing config to:
protocol websockets
listener 8080 127.0.0.1
protocol mqtt
Gives me a logfile that says:
1567707450: mosquitto version 1.6.2 starting
1567707450: Config loaded from C:\Program Files (x86)\mosquitto\mosquitto.conf.
1567707450: Opening ipv4 listen socket on port 8080.
1567707450: Opening websockets listen socket on port 1883.
(no extra crap)
After following up on answer no 1:
config:
protocol websockets
listener 1883 127.0.0.1
protocol mqtt
console:
WebSocket connection to 'ws://127.0.0.1:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
log:
1567716915: mosquitto version 1.6.2 starting
1567716915: Config loaded from C:\Program Files (x86)\mosquitto\mosquitto.conf.
1567716915: Opening ipv4 listen socket on port 1883.
1567716915: Opening websockets listen socket on port 1883.
1567716920: New connection from 127.0.0.1 on port 1883.
1567716920: Socket error on client <unknown>, disconnecting.
1567716920: New connection from 127.0.0.1 on port 1883.
1567716920: Socket error on client <unknown>, disconnecting.
5492: Error in poll: No error.
1567715492: Error in poll: No error.
1567715492: Error in poll: No error.
1567715492: Error in poll: No error.
1567715492: Error in poll: No error.
1567715492: Error in poll: No error.
Tried another websockets client (https://www.eclipse.org/paho/clients/js/utility/) --> Failed to connect: AMQJSC0001E Connect timed out.
I can't get websockets to work with any configuration / port...
Can anyone confirm that Websockets in Mosquitto (32bit version 1.6.2 or 1.6.4) for Win10 are working ?
Your first config file won't work because you have both native MQTT and Websockets both trying to listen on port 1883. (this is because the default listener starts on port 1883), Not 100% sure how this is possible unless it's some strange IPv6 thing on Windows.
The second, is just changing the default listener protocol to Websockets, which in theory should work, assuming you try and connect to port 1883 from the webpage.
The third one makes the default listener on port 1883 Websockets and native on 8080. Again should work assuming you are trying to connect to 1883
The simplest config to enable Websockets should look like this:
listener 9001 127.0.0.1
protocol websockets
This will leave the native default listener alone on port 1883 (listening on all interfaces, use bind_address 127.0.0.1 before the listener line to make it only listen on localhost) and start the Websocket listener on port 9001
This turned out to be quite the challenge.
First I tried to get things working using an online broker and client. Still not working. I then switched to another computer and everything was working fine.
Switching back to the original computer I decided to test if websockets where working at all by going to https://www.websocket.org/echo.html
From that moment on everything started to work. First the online broker and client and then also the local server and clients. I have no idea why...

New connection from ::1 on port 8883. Socket error on client <unknown>, disconnecting

I'm trying to connect/publish from broker to web, but it's not work.
I am already searching on the internet, but cannot find anything to solve it.
example when someone told me to untag bridge_protocol on mosquitto.conf , it's not work but get another error.
my mosquitto version is 1.4.9
I think something need to change on mosquitto.conf
When I try mosquitto -v -c mosquitto.conf
the result is :
mosquitto version 1.4.9
config loaded from mosquitto.conf
opening ipv4 and 6 socket on port 8883
new connection from :11 on port 8883.
socket error on client <unknown>, disconecting.
I can connect/publish from Broker to web.
I'm going to make a guess here...
It looks from the log that you have just changed the default port mosquitto is listening on from 1883 to 8883 most likely with the port configuration option.
You are then trying to connect to this from a webpage using the Paho JavaScript client.
This is failing because while you have changed the port you have not told mosquitto that it needs to use the websocket protocol for that port. This is because the Paho JavaScript client can only use MQTT over WebSockets not native MQTT.
The way to fix this is to reset the default port back to 1883 and then add a listener on port 8883 specifically set up to use WebSockets.
e.g. a mosquitto.conf like this:
port 1883
listener 8883
protocol websockets

How to set proxy for connecting of MQTT bridge?

I have a mosquitto broker run on a linux behind my company proxy.
I have been configured a bridge to AWS same following: (mosquitto.conf)
connection bridge
address ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com:8089
remote_username admin
remote_password password
topic abc/raspi01 both 0
bridge_cafile /etc/pki/tls/certs/nginx-selfsigned.crt
bridge_insecure false
But when I start mosquitto service with command:
service mosquitto restart
In log file, There are some errors:
1554356888: mosquitto version 1.5.5 starting
1554356888: Config loaded from /etc/mosquitto/mosquitto.conf.
1554356888: Opening ipv4 listen socket on port 1883.
1554356888: Opening ipv6 listen socket on port 1883.
1554356888: Warning: Address family not supported by protocol
1554356888: Connecting bridge bridge (ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com:8089)
1554356888: Error creating bridge: Name or service not known.
1554356888: Warning: Unable to connect to bridge bridge.
1554356901: New connection from 127.0.0.1 on port 1883.
1554356901: New connection from 127.0.0.1 on port 1883.
1554356901: New client connected from 127.0.0.1 as mqtt_fd05fada.b70918 (c1, k60).
1554356901: New client connected from 127.0.0.1 as mqtt_2a3a025d.6c941e (c1, k60).
1554356919: Connecting bridge bridge (ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com:8089)
1554356919: Error creating bridge: Name or service not known.
1554356950: Connecting bridge bridge (ec2-xxx-xxx-xxx-xxx.ap-northeast-1.compute.amazonaws.com:8089)
1554356950: Error creating bridge: Name or service not known.
I things the cause is my company proxy.
I have tried with settings in mosquitto.service, but it not resolve.
[Unit]
Description=Mosquitto MQTT v3.1/v3.1.1 Broker
Documentation=man:mosquitto.conf(5) man:mosquitto(8)
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
NotifyAccess=main
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Environment="HTTPS_PROXY=http://user:pass#proxyhost:8800"
Environment="HTTP_PROXY=http://user:pass#proxyhost:8800"
Environment="NO_PROXY=127.0.0.1,localhost"
Restart=on-failure
[Install]
WantedBy=multi-user.target
Can any body help me? Thank so much.
You can not use a HTTP proxy for MQTT bridge connection (or any native MQTT connection). MQTT is a totally different protocol.
Only MQTT over Websockets would work via a HTTP proxy, but you can not configure mosquitto to run a bridge with MQTT over Websockets.
If one uses HTTP CONNECT before sending connect
in net_mosq.c
rc = connect(*sock, rp->ai_addr, rp->ai_addrlen);
One can accomplish this.
HTTP CONNECT is protocol agnostic, it works on underlying TCP protocol.

Setting up rabbitMQ on AWS instance

I am having trouble setting up connection to a remote RabbitMQ server.
Basically i have the consumer and the server running on a remote ubuntu machine and want my local machine to act as publisher. I have ssh access to the remote machine. I have read the answer to the following and it seems it is possible
RabbitMQ on Amazon EC2 Instance & Locally?
However when i try to connect to the remote host
connection = AMQP.connect(:host => 'hostname')
puts "Connected to server...."
channel = AMQP::Channel.new(connection)
queue = channel.queue(queue_name, :auto_delete => true)
exchange = channel.default_exchange
I get a 'Detected TCP connection failure'. i ran netstat on the remote server and the port 5672 is listening.
Have you authorized the inbound traffic at port 5672 in the Security Group of your RabbitMQ server?
This may help:
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

Resources