1

I have MqttConnect.js file and mqttws31.js lib . I have to mqttws31.js all source code include my MqttConnect.js file, How it possible?.

when I copy everything from mqttws31.js and past mqttconnect.js file .that time this error occur:

ReferenceError: Messaging is not defined

if I try this way it is working fine :

<head>
    <meta charset="UTF-8">
    <title>Title of the document</title>
     <script src="http://www.hivemq.com/demos/websocket-client/js/mqttws31.js" type="text/javascript"></script> 

     <script src="MqttJS/MqttConnect.js"></script>

</head>

MqttConnect.js file code :

// Using the HiveMQ public Broker, with a random client Id

        var client = new Messaging.Client("broker.mqttdashboard.com",8000, "myclientid_" + parseInt(Math.random() * 100, 10));


        //Connect Options
        var options = {
            timeout: 60,
            keepAliveInterval:450, 
            cleanSession:false, 
            //Gets Called if the connection has sucessfully been established
            onSuccess: function () {

                alert("Connected:");

            },
            //Gets Called if the connection could not be established
            onFailure: function (message) {
                alert("Connection failed -: " + message.errorMessage);
            }
        };

        function Connect(){
            try {

            client.connect(options)



            }
            catch(err){
                alert(err.message);

            }

        }

mqttws31.js code:

http://www.hivemq.com/demos/websocket-client/js/mqttws31.js

UPDATE

where I want use this , there have no html page

  • 1
    Have you copied everything from `mqttws31.js` file to `MqttConnect.js` at the top? I mean content of `mqttws.js` file first then `MqttConnect.js` file. – Savaratkar Feb 25 '16 at 04:27
  • I have copied everything at the the bottom , well do it –  Feb 25 '16 at 04:30
  • 1
    Yes, content of `MqttConnect.js` defines `Messaging` function which is used in `MqttConnect.js` file. Thus content of `MqttConnect.js` file should get loaded first. – Savaratkar Feb 25 '16 at 04:56
  • still an error ReferenceError: "window" is not defined. –  Feb 25 '16 at 05:01

3 Answers3

1

This may be due to a quirk of how JavaScript loads. You can find a good example of how it should be done in this answer.

The quick answer is to place the loading of both JavaScript files into the body of the HTML document hosting them, with the MQTT library above your script.

Do NOT just copy the library into your own file, that's very poor form and a copyright violation if you don't credit the library's source properly.

Community
  • 1
  • 1
Rodinga
  • 41
  • 3
  • but i have no alternative way . and still an erorr ReferenceError: "window" is not defined. –  Feb 25 '16 at 05:04
  • 'window' is used in the library to get the web page's DOM, essentially meaning the page hasn't finished loading when the script is executed. You can try adding 'defer' to the script tags, which might help solve this because it delays the JS load until after the page has loaded – generally. – Rodinga Feb 25 '16 at 05:23
  • where I want use this , there have no html page –  Feb 25 '16 at 05:59
  • 1
    Then this library won't work. It's intended to use web sockets as implemented by a web browser, so if you're using this server side (as you indicated below) then you will need to find another solution for your specific environment. – Rodinga Feb 25 '16 at 07:00
1

Copy content of mqttws31.js into MqttConnect.js at the top (not at the bottom) and then load MqttConnect.js file:

<head>
    <meta charset="UTF-8">
    <title>Title of the document</title>
    <script src="MqttJS/MqttConnect.js"></script>

</head>

I tried this myself, I am not getting any error. (window is undefined)

Savaratkar
  • 1,974
  • 1
  • 24
  • 44
  • where I want use this , there have no html page –  Feb 25 '16 at 05:39
  • Ok, where do you want to use it? – Savaratkar Feb 25 '16 at 05:39
  • I want to use it, IBM Mobilefirst server side in http adapter as this qusentions ansewer http://stackoverflow.com/questions/35579623/include-javascript-library-on-the-server-side-in-mobilefirst –  Feb 25 '16 at 05:45
  • Ohhk, understood. I believe then you cannot include this js file. I am not sure how similar is the environment of MobileFirst to browsers – Savaratkar Feb 25 '16 at 05:50
  • no way !.can you suggest how can i include this file . little bit edit? –  Feb 25 '16 at 05:58
0

There is a dependency between the two files, that is, there is code in MqttConnect.js which needs the code in mqttws31.js in order to work properly. So I'm assuming you pasted the contents of mqttws31.js at the end of MqttConnect.js. Pasting the contents of mqttws31.js at the beginning of MqttConnect.js should fix this. Your MqttConnect.js should look like

// Contents of mqttws31.js ...
// Contents of MqttConnect.js ...