简体   繁体   中英

send a message between a webpage to RabbitMq in raspberry pi

I'm trying to send a message between a webpage to RabbitMq in raspberry pi. The send.js and receive.js from the RabbitMq tutorial works fine. However, when adding a link to the javascript file in the HTML is doesn't work

<HTML>  
<head>  
<script type = "text/javascript" src="send.js"></script>  
</head>  
<body>  
<p>Click the following button to see the function in action</p>  
<input type = "button" onclick = "myfunction()" value = "Display">  
</body>  
</html>  

Thesend.js is the one in the website's tutorial. The only change I made is adding the function heading

function myfunction () {


  var amqp = require('amqplib/callback_api');

  amqp.connect('amqp://localhost', function(error0, connection) {
    if (error0) {
       throw error0; 
         ....
            .....
              ....

IS there anything that I'm missing?

require is a feature of CommonJS modules. This is the default module system used by Node.js and is not supported by web browsers.

While some modules can be converted for use in browsers using a bundler such as Webpack or Parcel, the one you are using is clearly marked as being for Node.js and almost certainly depends on APIs that are available in Node.js but not in browsers.

Your code will not run in a browser.

You could write a web service using Node.js (eg using the Express.js module) which uses amqplib and then access it using the fetch API which is natively supported in browsers. Alternatively, if you need the server to be able to initiate a message, useWebsockets .

The idea of having a live connection via websites to a RabbitMQ service goes around WebSockets. To this approach we need a plugin installed on the RabbitMQ service, then on the client side, opening a WebSocket to connect with the plugin. The plugin is a kind of bridge between WS and the broker.

According to my experience, Paho.js and STOMP are two existing solutions. Paho.js is limited to connecting to topics only, while STOMP is a complete utilitarian tool.

To enable the STOMP plugin on your RabbitMQ server:

rabbitmq-plugins enable rabbitmq_stomp

Then install the bridge too:

rabbitmq-plugins enable rabbitmq_web_stomp

Now your server is ready to be used. Let's jump into the client side. Download this file and put it somewhere in your project. Now the socket connection part:

import { Stomp } from './stomp';

var ws = new WebSocket('ws://yourServerAddress:15674/ws');
var client = Stomp.over(ws);

var on_receive =  function(data) {
    console.log('received:', data.body);
};
var on_connect = function() {
    console.log('connected');
    client.subscribe('/amq/queue/your-queue', on_receive)
};
var on_error =  function() {
    console.log('error');
};

client.onreceive = on_receive;
client.connect('yourUsename', 'thePasswd', on_connect, on_error, '/');

function send(payload, headers={}){
  client.send('/amq/queue/your-queue', headers, JSON.stringify(payload));
}
  • Keep in mind I wrote this code to connect straight to a queue. For topics just change the subscription endpoint to /amq/topic/your-topic

You can see the full documentation here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM