0

I am trying to setup a webserver that is able to send messages to a message queue. For this, I want to use STOMPIT. I have setup a basic nodejs webserver with an index.html, main.js and app.js but can't get the stompit library working in my main.js.

How can I use the library (imported with the script tags in the html) in my main.js?

Parts of the code: index.html:

<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.js"></script>
<script src="js/main.js"></script>

main.js:

    stompit.connect(connectOptions, function(error, client) { //stompit is undefined here

        if (error) {
            console.log('connect error ' + error.message);
            return;
        }

        const sendHeaders = {
            'destination': '/queue/test',
            'content-type': 'text/plain'
        };

        const frame = client.send(sendHeaders);
        frame.write(msg);
        frame.end();
    })
Niels_
  • 76
  • 7
  • Have you tried installing stomp npm module using 'npm install stomp` and then importing it into the js file? – user8115627 Apr 08 '22 at 07:26
  • I tried to do this, but then I get an error in my browser saying, "Uncaught ReferenceError: require is not defined" – Niels_ Apr 08 '22 at 07:39
  • 1
    if you are using it for web browser then follow this https://stomp-js.github.io/guide/stompjs/using-stompjs-v5.html#in-web-browser – user8115627 Apr 08 '22 at 07:50

1 Answers1

0

You have to choose a module on npmjs.com, install it and use it in your scripts. For example with stomp module (there are others on npmjs) : https://www.npmjs.com/package/stompjs

Install it with:

npm install --save stompjs

Import it in your scripts:

const Stomp = require('stompjs');

You can find other details about using it on: https://www.npmjs.com/package/stompjs

Alaindeseine
  • 3,260
  • 1
  • 11
  • 21
  • I tried to add this to my main.js, but then I get an error in my browser saying, "Uncaught ReferenceError: require is not defined". if I add it to my app.js, i dont know how to reach `Stomp` in my main.js – Niels_ Apr 08 '22 at 07:40
  • Can you confirm that main.js is part of you node.js application and not a client side script ? – Alaindeseine Apr 08 '22 at 07:50
  • Oh sorry I wasnt clear, it is part of the client side script. This script is supposed to send a message to the message queue with stomp on a button click – Niels_ Apr 08 '22 at 07:57
  • 1
    Ok, that's what I thought. You can't require node.js modules in client side script because require is not known by browsers javascript engines. Some package can be browserified. Read this : https://stackoverflow.com/questions/19059580/client-on-node-js-uncaught-referenceerror-require-is-not-defined for detailed explanations – Alaindeseine Apr 08 '22 at 08:11