1

I have searched both the web and stockexchange for an explanation of this strange behavior I experience when connecting to MQTT. But I couldn't find any similar case and I would like to understand where the issue comes from.

So, I have set up a Mosquitto MQTT broker on my Raspberry Pi to listen on port 1883. I have also set up port forwarding on my router, so I can reach the Pi from outside my home network (though I can reproduce the behavior below with a public server like broker.hivemq.com as well). When I execute the following Node.js script, I can connect to the broker and subscribe and publish messages, it works perfectly:

const mqtt = require('mqtt') 
const client = mqtt.connect('tcp://my.address.net:1883')
client.on('connect', () => {    
  console.log('Connected!')
  client.subscribe("chat")
})

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', function(line){
    client.publish('chat',line)
})

client.on('message', (topic, message) => {  
    console.log('>> '+message)
})

However, when I leave out the tcp:// protocol and have

const client = mqtt.connect('my.address.net:1883')

as the host address I don't get a connection, but also no error message. The program just hangs until I terminate it. I don't understand that. Isn't MQTT using TCP by default?

Does that have to do with my client or with my broker? Could it have something to do with my system (OSX)?

Maybe this has nothing to do with it, but a similar behavior I get when I use the Paho MQTT package in Python, and this is actually the more important case for me, because here I don't get it to run at all. This is my code:

import paho.mqtt.client as paho

def on_connect(client, userdata, flags, rc):
    print("connected")
    client.disconnect()

def on_disconnect(client, userdata, rc):
    print("disconnected")

client = paho.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect("my.address.net", 1883)

Using this version without protocol, I don't get any response. The program runs for a second and then terminates. If I use the protocol

client.connect("tcp://my.address.net", 1883)

I get the error

socket.gaierror: [Errno 8] nodename nor servname provided, or not known

I don't know what I am missing.

Can someone explain the difference of stating the tcp:// protocol in the address vs. leaving it out?

2 Answers 2

1

This is two question.

For the python part, the python client expects host and port separately as its connection arguments. It is not a URI. Hence no TCP//.

The python program does exactly what you ask - connect, then exit. You'll have to add more code beneath the connect call if you want it to do anything else. What that code is depends on what you want to do, but loop_forever() is a good start.

The other client expects a URI. I believe it uses TCP/SSL to distinguish between plain and encrypted connections.

1
  • Thank you for the explanation with the URI. The python code actually worked, like you said! Once I added the loop_forever(), it also displayed the 'connected' and 'disconnected' messages, this was what got me so confused in the first place.
    – Nejesis
    May 16, 2016 at 16:57
0

The 2 libraries are requiring different things.

The NodeJS library is asking for a URI which includes

  1. a schema ("tcp://")
  2. a hostname/IP address ("local host")
  3. a port number (":1883")

The host and port can easily be parsed from this.

Where as the Python library is explicitly asking for 2 separate things

  1. the hostname
  2. the port

These are taken as separate variables.

This just down to different authors choosing different approaches to gather information.

1
  • Thank you for your answer, now I know what an URI is all about :)
    – Nejesis
    May 16, 2016 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.