Warm tip: This article is reproduced from serverfault.com, please click

How to access rascal.js publishing from different files?

发布于 2020-11-29 13:15:28

I am trying to use rascal.js with my node.js app to publish messages via RabbitMQ.

To simplify things:

I got the standard rascal.js file which looks similar to their docs. And I got a controller.js file which receives http request for saving some data. Then this controller.js file calls 2 functions on different files. "SendMail" and "NotifyUser". Both of those functions needs to publish messages to specific queues.

The question how can I access the Rascal.js publish logic from those 2 files? I could not find any examples. Maybe the publish logic suppose to be activated differently? By triggers or events of sort?

And so far the only solution I could use was creating another separate function in my rascal.js file which opens a connection specifically for publishing. It uses input params to determine the name of the queue and the message to be published to it and at the end it closes the connection. This seems to be a bad solution since every request will open a new connection with rascal.js.

this is my first post in this subject - How to manage publish connection per request on rabbitmq(rascal.js)

Questioner
Ivgi
Viewed
0
lifeisfoo 2020-11-29 21:51:54

If you're using an express like http server (with middlewares) you can bind the broker to each request object and the use it in your controller code.

This is an example code using express:

your-app.js file:

// import your controller functions
const signup = require('./controller')

let broker = null;

(async () => {
  try {
    broker = await Broker.create(config);
    // your broker setup logic
})();

// your http app setup logic...
app = express(); 

//add the broker to each request object
app.use((req, res, next) => {
  req._broker = broker;
  next();
});

app.use('/signup', signup);

controller.js file:

module.exports = (req, res, next) => {
  // TODO: your controller logic here
  // send the message using the available broker
  req._broker.publish('send_email', 'Hello new user!');
  next();
});