@amplication/plugin-broker-redis
v2.0.7
Published
Use a Redis message broker in the service generated by Amplication.
Downloads
34,542
Keywords
Readme
@amplication/plugin-broker-redis
Use a Redis message broker in the service generated by Amplication.
Purpose
This plugin adds the required code to use Redis as a message broker in the service generated by Amplication.
It updates the following parts:
- Adds the required dependencies to
package.json
. - Adds the required environment variables to
.env
. - Adds a module
RedisModule
with a serviceRedisProducerService
that can be used to emit messages and a controller to respond to topic events. - Adds the
RedisModule
configured to use Redis to theapp.module.ts
module imports list. - Adds the required redis service to the
docker-compose.yml
asdocker-compose.dev.yml
files.
Configuration
The port
setting is the port that will be used for the url of the redis server.
The host
setting is the host that will be used for the url of the redis server.
The retryAttempts
setting is the number of times to retry a message.
The retryDelay
setting is the delay between each message retry attempt.
The enableTls
setting is set to true when TLS should be used.
If no configuration is provided the .amplicationrc.json file will use be used as the default values.
{
"host": "localhost",
"port": 6379,
"retryAttempts": 3,
"retryDelay": 3,
"enableTls": false
}
For more information about TLS configuration, check the ioredis docs https://github.com/redis/ioredis#tls-options
Scripts
build
Running npm run build
will bundle your plugin with Webpack for production.
dev
Running npm run dev
will watch your plugin's source code and automatically bundle it with every change.
test
Running npm run test
will run the plugin's test suite.
Usage
This plugin provides you with a Redis broker module that you can use in your service. To configure, set the following environment variables:
REDIS_BROKER_HOST - the host that will be used in the url of the Redis server.
REDIS_BROKER_PORT - the port that will be used in the url of the Redis server.
REDIS_BROKER_RETRY_ATTEMPTS - The number of times to retry a message.
REDIS_BROKER_RETRY_DELAY - The delay between each message retry attempt.
REDIS_BROKER_ENABLE_TLS - Set to "true" when TLS should be used.
An example of how to use the Redis module:
In a controller:
export class ModelController extends ModelControllerBase {
constructor(protected readonly service: ModelService,
private redisService: RedisProducerService) {
super(service);
}
@Get()
async respondToUser(): Promise<void> {
await this.redisService.emit(MessageBrokerTopics.FirstTopic, { message: "hello, world!" });
return "Done";
}
}
Customization of the Redis controller to perform desired tasks:
@Controller("redis-controller")
export class RedisController {
@EventPattern("firstTopic")
async onFirstTopic(
@Payload()
message: RedisMessage
): Promise<void> {
console.log("Received message:", message);
}
}