Reputation: 6177
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);
...
}
Upvotes: 2
Views: 816
Reputation: 59816
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)
Upvotes: 1