@stijlbreuk/nestjs-redis-streams-transport
v10.0.4
Published
A transport layer for NestJS microservices
Downloads
231
Readme
Description
Redis Streams server strategy and client module for Nest.js version 10.X which uses ioredis under the hood.
Warning
At the moment the redis streams strategy only supports event based microservice communication.
Installation
$ npm i --save npm i @stijlbreuk/nestjs-redis-streams-transport
Usage
To use the Redis Streams transporter, pass the following options object to the createMicroservice()
method:
import { NestFactory } from '@nestjs/core';
import { RedisStreamStrategy } from '@stijlbreuk/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();
Client
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
with the REDIS_STREAMS_CLIENT_PROXY
token.
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],
}),
Stay in touch
- Author - Mark op 't Hoog
License
Nest is MIT licensed.