Redis Streams server strategy and client module for Nest based on the node-redis package.
At the moment the redis streams strategy only supports event based microservice communication.
$ npm i --save npm i @mark_hoog/redis-streams-transport
To use the Redis Streams transporter, pass the following options object to the createMicroservice()
method:
import { NestFactory } from '@nestjs/core';
import { RedisStreamStrategy } from '@hoogie/redis-streams-transport';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.createMicroservice<CustomStrategy>(AppModule, {
strategy: new RedisStreamStrategy({
// consumerGroup: 'example-group',
// consumer: 'example-consumer',
}),
});
app.listen(() => console.log('Microservice is listening'));
}
bootstrap();
To create a client instance with the RedisStreamsClientModule
, import it and use the register()
method to pass an options object with the redis connect properties.
@Module({
imports: [
RedisStreamsClientModule.register({
url: 'redis://localhost:6379',
}),
]
...
})
Once the module has been imported, we can inject an instance of the ClientProxy
constructor(
private readonly client: ClientProxy,
) {}
Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync()
method, that provides a couple of various ways to deal with async data.
1. Use factory
RedisStreamsClientModule.registerAsync({
useFactory: () => ({
url: 'redis://localhost:6379',
}),
});
Obviously, our factory behaves like every other one (might be async
and is able to inject dependencies through inject
).
RedisStreamsClientModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
url: configService.get('REDIS_URL', undefined),
password: configService.get('REDIS_PASSWORD', undefined),
}),
inject: [ConfigService],
}),
- Author - Mark op 't Hoog
Nest is MIT licensed.