paho.mqtt.javascript icon indicating copy to clipboard operation
paho.mqtt.javascript copied to clipboard

Creating new client throws "Cannot read property 'Client' of undefined"

Open dandonahoe opened this issue 8 years ago • 4 comments
trafficstars

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:

screen shot 2017-02-14 at 7 47 39 pm

So I can't figure out what I'm doing wrong. About 95% of this is AWS example code. Any ideas?

Thanks!

dandonahoe avatar Feb 15 '17 00:02 dandonahoe

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

jpwsutton avatar Feb 20 '17 10:02 jpwsutton

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)

nbalagopal avatar Feb 09 '18 21:02 nbalagopal

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");

bertmelis avatar Dec 14 '18 10:12 bertmelis

I second to what bertmelis said. This fixes the problem. How I got to it though:

  1. 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

  2. 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.

TiMladenov avatar Oct 01 '19 14:10 TiMladenov