
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
@rhtml/amqp
Advanced tools
@rhtml/amqp is an AMQP (Advanced Message Queuing Protocol) integration library designed for seamless interaction with message brokers such as RabbitMQ. It allows developers to easily publish and subscribe to messages, integrating AMQP functionalities into their applications with minimal setup.
To use @rhtml/amqp in your project, install it via npm:
npm i @rhtml/amqp
You can set up the AMQP connection in your application by using the AmqpModule.forRoot method. This allows you to configure the connection settings such as protocol, hostname, port, credentials, and vhost.
import { FastifyModule } from '@rhtml/fastify';
import { AmqpModule } from '@rhtml/amqp';
@Module({
imports: [
FastifyModule.forRoot({...}),
AmqpModule.forRoot({
protocol: 'amqp', // Default protocol: 'amqp'
hostname: 'localhost', // Hostname of the RabbitMQ server
port: 5672, // Default AMQP port: 5672
username: 'amqp user', // AMQP username (default: 'guest')
password: 'amqp password', // AMQP password (default: 'guest')
vhost: '', // Virtual host to use (default: empty string)
/**
* Optional
* Set the prefetch count for this channel.
* The count given is the maximum number of messages sent over the channel that can be awaiting acknowledgement;
* once there are count messages outstanding,
* the server will not send more messages on this channel until one or more have been acknowledged.
*/
prefetchCount: 1
}),
],
})
export class AppModule {}
Integrate AMQP with your Fastify controllers for message subscription and publishing:
import { Controller, Route } from '@rhtml/fastify';
import { FastifyRequest } from 'fastify';
import {
AmqpChannel,
AmqpService,
ConsumeMessage,
Subscribe,
} from '@rhtml/amqp';
@Controller({
route: '/',
})
export class MyController {
constructor(private amqpService: AmqpService) {}
// Subscription handler for consuming messages
@Subscribe({
name: 'test-queue',
consumeOptions: {
noAck: true, // Automatically acknowledge messages
},
})
withAutoAcknowledge(data: ConsumeMessage, channel: AmqpChannel) {
// Parse the incoming message
const message = JSON.parse(data?.content.toString());
console.log(message);
// Output: { test: '123' }
}
@Subscribe({
name: 'test-queue',
consumeOptions: {
noAck: false,
},
})
withCustomAcknowledge(data: ConsumeMessage, channel: AmqpChannel) {
const message = JSON.parse(data?.content.toString());
setTimeout(() => {
// Long Running Job can be parsing some file
console.log(message);
channel.ack(data!);
}, 10000);
}
// Route to trigger message publication
@Route({
url: '',
method: 'GET',
})
async myRouteTrigger(req: FastifyRequest) {
// Publish a message to the 'test-queue'
this.amqpService.publish('test-queue', { test: '123' });
}
}
import { AmqpConnection } from '@rhtml/amqp'
import { InjectionToken, Module } from '@rhtml/di'
import { Fastify } from '@rhtml/fastify'
import { Connection } from 'amqplib'
import { FastifyInstance } from 'fastify'
@Module({
providers: [
/* Close the AMQP Connection when server stops */
{
provide: new InjectionToken(),
deps: [Fastify, AmqpConnection],
useFactory: (fastify: FastifyInstance, connection: Connection) =>
fastify.addHook('onClose', () => connection.close()),
},
]
})
import { Channel, AmqpConnection, Connection } from '@rhtml/amqp';
import { InjectionToken } from '@rhtml/di';
/**
* Injection for MyAmqpChannel
*/
export const MyAmqpChannel = new InjectionToken<Channel>();
@Module({
providers: [
{
provide: MyAmqpChannel,
deps: [AmqpConnection],
useFactory: async (connection: Connection) => {
const channel = await connection.createChannel();
await channel.prefetch(1);
return channel;
},
},
],
bootstrap: [WeatherDataController],
})
export class AppModule {}
@Controller({
route: '/',
})
export class WeatherDataController {
constructor(
private weatherDataService: WeatherDataService,
private amqpService: AmqpService
) {}
@Subscribe({
queue: 'my-queue-with-acknowledgment',
consumeOptions: {
noAck: false,
},
channel: MyAmqpChannel,
})
async preParseRequest(message: ConsumeMessage, channel: AmqpChannel) {
// You need to manually acknowledge the message or it is threated as `unacked` in RabbitMQ Dashboard
channel.ack(message);
}
@Subscribe({
queue: 'my-queue-without-acknowledgment',
channel: MyAmqpChannel,
})
async preParseRequest(message: ConsumeMessage, channel: AmqpChannel) {
// Do something here and message will be auto acknowledged
}
}
Subscription: The controller subscribes to the test-queue using the @Subscribe decorator. Any message sent to this queue is automatically consumed by the mySubscription method.
Route Trigger: The myRouteTrigger route is exposed as a GET endpoint. When accessed, it publishes a message ({ test: '123' }) to the test-queue.
Message Consumption: The subscription method processes the published message, logs its content, and acknowledges it if required.
Extend the configuration to include custom retry policies, connection recovery, or specific consume behaviors tailored to your application.
This library is licensed under the MIT License. Feel free to use, modify, and distribute it for your projects.
FAQs
AMQP module with decorators
The npm package @rhtml/amqp receives a total of 16 weekly downloads. As such, @rhtml/amqp popularity was classified as not popular.
We found that @rhtml/amqp demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.