eclipse / paho.mqtt.javascript

paho.mqtt.javascript
Other
1.14k stars 468 forks source link

Can I change the IP Address of a Paho.MQTT.Client after creating it? #160

Open PeterCAN opened 6 years ago

PeterCAN commented 6 years ago

Hi All,

I'm writing an web application where I want a single Paho.MQTT.Client to be able to disconnect from a MQTT broker, and connect it to another MQTT broker with a different IP address.

Is this possible?

I've tried myClient._setHost("a.b.c.d"); and that returns 'Unsupported', even after a call to myClient.disconnect().

So do I have to delete my existing Paho.MQTT.Client and create a new one with a different IP address in order to switch between MQTT brokers?

jpwsutton commented 5 years ago

Hi @PeterCAN, Once the client has been instantiated, the hostname can no longer be changed. You'll need to create a new client before you can use a new hostname.

One important distinction with this though: if you use the hosts field in the connection options, you can define multiple hosts that the client can cycle through if the host it attempts to connect to / is connected to is no longer available (http://www.eclipse.org/paho/files/jsdoc/Paho.MQTT.Client.html)