I am learning nestjs microservice,
What command can I use?
const pattern = { cmd: 'get' };
this.client.send<any>(pattern, data)
And how can I receive data from redis?
constructor(private readonly appService: AppService) {}
@Client({
transport: Transport.REDIS,
options: {
url: 'redis://127.0.0.1:6379',
},
})
client: ClientProxy;
@Get()
getHello(): any {
const pattern = { cmd: 'get foo' }; //Please write sample code here in document
const data = '';
return this.client.send<any>(pattern, data);
}
There are two sides you need to separate. They can be part of one nest.js application (e.g. hybrid application) or be in several different nest.js applications:
Client
The client broadcasts messages on a topic/pattern and receives a response from the receiver(s) of the broadcasted message.
First, you have to connect your client. You can do that in
onModuleInit. In this example,ProductServicebroadcasts a message when a new product entity is created.Keep in mind, that
this.client.sendreturns anObservable. This means, nothing will happen until yousubscribeto it (which you can implicitly do by callingtoPromise()).Pattern Handler
The pattern handler consumes messages and sends a response back to the client.
Of course, a param handler could also be a client and therewith both receive and broadcast messages.