Hello folks I am stuck in a problem I will start by explaining what I am doing,
I have created 2 nodejs server one is publisher and other is subscriber and both servers would perform a math task like addition and multiplication
So i have created a rest api for addition and multiplication I am also getting the desired result if i start publisher and subscriber server and hit the addition / multiplication endpoint I am getting the desired result at the subscriber end
But I am not getting the idea of how can I create 2 copies of publisher and 3 copies of sub-worker/subscriber where pub1 and pub2 status will be different and upon completion the worker now has to inform the publishers about the result
Also it should tell all the past tasks and their result or pending status to publisher and pub server should keep task list in local temp file
we can also use docker to create multiple copies but dont have idea how to do it ?
I am also sharing my code with you all
Any help with example would be appreciated
Thanks in advance!
Publisher.js file
const express = require("express");
const amqp = require("amqplib");
const app = express();
const bodyParser = require("body-parser");
const PORT = process.env.PORT || 3000;
let channel, connection;
app.use(express.json());
app.get("/math-task/sum", (req, res) => {
let inputOfA = parseInt(req.body.a);
let inputOfB = parseInt(req.body.b);
let sum = Number(inputOfA + inputOfB);
sendData(sum); // pass the data to the function we defined
console.log("A message is sent to queue");
res.send("Message Sent For Addition:" + Number(sum)); //response to the API request
});
app.get("/math-task/mul", (req, res) => {
let inputOfA = parseInt(req.body.a);
let inputOfB = parseInt(req.body.b);
let product = Number(inputOfA * inputOfB);
sendData(product); // pass the data to the function we defined
console.log("A message is sent to queue");
res.send("Message Sent For Multiplication:" + Number(product)); //response to the API request
});
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.listen(PORT, () => console.log("Server running at port " + PORT));
async function connectQueue() {
try {
connection = await amqp.connect("amqp://localhost:5672");
channel = await connection.createChannel();
await channel.assertQueue("test-queue");
} catch (error) {
console.log(error);
}
}
async function sendData(data) {
// send data to queue
await channel.sendToQueue("test-queue", Buffer.from(JSON.stringify(data)));
// close the channel and connection
await channel.close();
await connection.close();
}
connectQueue();Subscriber.js file
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3001;
app.use(express.json());
app.listen(PORT, () => console.log("Server running at port " + PORT));
const amqp = require("amqplib");
var channel, connection;
connectQueue() // call the connect function
async function connectQueue() {
try {
connection = await amqp.connect("amqp://localhost:5672");
channel = await connection.createChannel()
await channel.assertQueue("test-queue")
channel.consume("test-queue", data => {
console.log(`${Buffer.from(data.content)}`);
channel.ack(data);
})
} catch (error) {
console.log(error);
}
}from Nodejs publisher and subscriber in rabbitmq
No comments:
Post a Comment