paho.mqtt.javascript
paho.mqtt.javascript copied to clipboard
Creating new client throws "Cannot read property 'Client' of undefined"
All is good an well until this line:
var client = new Paho.MQTT.Client(requestUrl, clientId);
Where I get the error message
Uncaught Error: Cannot read property 'Client' of undefined
at App.initClient (eval at <anonymous> (bundle.js:2341), <anonymous>:201:53)
at App.proxiedMethod (eval at <anonymous> (bundle.js:3565), <anonymous>:44:30)
at eval (eval at <anonymous> (bundle.js:2341), <anonymous>:185:22)
at eval (eval at <anonymous> (bundle.js:4603), <anonymous>:123:23)
at Response.eval (eval at <anonymous> (bundle.js:4651), <anonymous>:258:7)
at Request.eval (eval at <anonymous> (bundle.js:4729), <anonymous>:358:18)
at Request.callListeners (eval at <anonymous> (bundle.js:4687), <anonymous>:105:20)
at Request.emit (eval at <anonymous> (bundle.js:4687), <anonymous>:77:10)
at Request.emit (eval at <anonymous> (bundle.js:4729), <anonymous>:671:14)
at Request.transition (eval at <anonymous> (bundle.js:4729), <anonymous>:22:10)
When debugging on the line where Client was supposed to be instantiated, devtools show this:

So I can't figure out what I'm doing wrong. About 95% of this is AWS example code. Any ideas?
Thanks!
Looks like you're not supplying the correct arguments. Take a look at the examples and documentation here:
- https://github.com/eclipse/paho.mqtt.javascript/blob/master/utility/utility.js
- https://github.com/eclipse/paho.mqtt.javascript/blob/master/README.md
- https://www.eclipse.org/paho/files/jsdoc/symbols/Paho.MQTT.Client.html#constructor
You basically need to supply either:
- Hostname, port, path and client ID
- Or Hostname, port, client ID
Are you using the paho-mqtt npm module? If that's the case, I had to drop the "MQTT" in the classname
import Paho from 'paho=mqtt'
var client = Paho.Client(mqtt_server, Number(mqtt_port), client_name)
Encountered this issue because I simply used the example code from the README page. Although it is mentioned in the paragraph below, the example should be updated. (because there are people like me that stop reading once they found their answer)
// Create a client instance
var client = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");
should be
// Create a client instance
var client = new Paho.Client(location.hostname, Number(location.port), "clientId");
I second to what bertmelis said. This fixes the problem. How I got to it though:
-
I was using older cdnjs versions of the route for the Paho MQTT modules: https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js
-
These, it seems, are missing the 'reconnect' property from the connection 'options' dictionary. So, I got the newest cdnjs version, which is: https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.1.0/paho-mqtt.js
This one has the 'reconnect' property, but for it to work, as bertmelis pointed out, you need to use 'Paho.Client', instead of 'Paho.MQTT.Client' to access the constructor.