mr-publisher
v0.2.3
Published
Effortlessly publish your data to RabbitMQ with a clean.
Readme
Mr.Publisher
Effortlessly publish your data to RabbitMQ with a clean, TypeScript-friendly API.
Introduction
The MrPublisher layer allows you to achieve significant benefits in the following parts of writing code:
- Make development process clear for all participants
- Speed up the development of production-ready projects
- Avoid complexity
- Reduce coupling
So, developers and Mr. Publisher have to be friends🤝 forever at least for reasons outlined above.
Installation
Just one step.
npm i mr-publisherAnd use it where you need it.
import { MrPublisher } from 'mr-publisher';Setup
To get started, simply connect a RabbitMQ channel to your MrPublisher and define your list of possible queues.
import { MrPublisher } from 'mr-publisher';
import { rabbitMQ } from './rabbitMQ';
enum MessageBrokerQueue {
coreMessageCreated = 'core.message.created',
telegramTelegramMessagesCreated = 'telegram.telegramMessage.created',
}
export function Publisher<Payload extends object>() {
return class Publisher extends MrPublisher<Payload, MessageBrokerQueue>() {
async setupChannel() {
return rabbitMQ.getChannel();
}
};
}You can configure RabbitMQ however you prefer for use with MrPublisher.
import amqp, { Channel, ChannelModel } from 'amqplib';
let connection: ChannelModel;
let channel: Channel;
async function getConnection(): Promise<amqp.ChannelModel> {
if (!connection) {
const rabbitUrl = process.env.RABBITMQ_URL ?? '';
connection = await amqp.connect(rabbitUrl);
}
return connection;
}
async function getChannel(): Promise<Channel> {
if (!channel) {
const conn = await getConnection();
channel = await conn.createChannel();
}
return channel;
}
const rabbitMQ = {
getConnection,
getChannel,
};
export { rabbitMQ };Overview
This section contains a simple case that shows us an example of Mr.Publisher implementation. Let's take a quick look at the following piece of code:
import { MessageBrokerQueue } from '../utils/mr.publisher';
interface Payload {
message: {
uuid: string;
};
}
export class SampleMessageCreatedPublisher extends Publisher<Payload>() {
queueName = MessageBrokerQueue.coreMessageCreated;
}Now you can safely publish your data from anywhere in your code:
const message = { uuid: '94e95f6f-b49f-482f-96d5-410b21edf9c0' };
await SampleMessageCreatedPublisher.publish({
payload: {
message: {
uuid: message.uuid,
},
},
});As you can see, the code is pretty simple and easy to user.
Now let's see how we may use it in the positive scenario:
Let's make a conclusion.
⚠️ At this moment you probably would like to see an integration of the module to something more ready to use. And specifically for this purpose 🐨 Mr.Koa boilerplate exists.
Conclusion
Using publishers makes your workflow simpler, more organized, and efficient. Mr.Publisher offers an intuitive interface so you can enjoy these benefits without any hassle.
Give it a try!
