https://github.com/leroyanders/amqplib-rpc
RabbitMQ RPC Helper
https://github.com/leroyanders/amqplib-rpc
Last synced: 2 months ago
JSON representation
RabbitMQ RPC Helper
- Host: GitHub
- URL: https://github.com/leroyanders/amqplib-rpc
- Owner: leroyanders
- Created: 2023-03-11T23:01:08.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-03-11T23:28:15.000Z (over 2 years ago)
- Last Synced: 2025-02-07T13:18:04.832Z (4 months ago)
- Language: JavaScript
- Size: 850 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# RabbitMQ amqplib RPC helper class (Reply-to)
## Instalation```bash
npm install amqplib
```## Server side (Provider)
```javascript
const Provider = require("./provider")new Provider({
queue: "example4",
// this function will reply consumers messages
responseFn: async (message, reply) => {
switch (message) {
case "command1":
return "Hi there!";
break;
case "command2":
return {status: 200};
break;
default:
return {result: 404, reason: "not found command."};
break;
}
},
}).connect("amqp://localhost");
```Note you should use responseFn to reply the answer, if you don't wanna answer, just return nothing.
## Client side (Consumer)
```javascript
const Consumer = require("./consumer")new Consumer({
queue: "example4",
}).then((client) => {
client
.connect("amqp://localhost")
.then((client) => {
return client.send({ text: "hello" });
})
.then((response) => {
console.log(response);
});
});
```### Express.js example
```bash
npm install express
``````javascript
const express = require("express");
const app = express();
const Consumer = require("./consumer")app.get("/test", async (req, res) => {
const client = await new Consumer({
queue: "example4"
});
// Send message and receive the answer from provider
const response = await client.connect("amqp://localhost")
.then((client) => client.send("hello"))
.then(res);res.send(response);
})app.listen(3000, () => {
console.log(`Server is running on port 3000`);
});
```### Advanced parameters:
- **queue**: *required.*, Set your queue.
- **correlationId**: Set correlation ID for receiver.
- **replyTo**: *required.* Client to receive reply from provider.
- **contentType**: "*application/json*" or "*application/textplain*", it used for format message, in any case it will do automatically.
- **responseType**: "application/json" or "application/textplain", it used for format message, in any case it will do automatically.
- **connectionAdress**: Connection adress, for example: ```amqp://localhost/```