There is another way to work with Nest microservices. Instead of direct TCP communication, we could use amazing Redis feature - publish / subscribe.

Of course before you can use it, it is obligatory to install Redis.

Create microservice

To create Redis Microservice, you have to pass additional configuration in NestFactory.createMicroservice() method.

const app = NestFactory.createMicroservice(
        transport: Transport.REDIS, 
        url: 'redis://localhost:6379' 
app.listen(() => console.log('Microservice listen on port:', 5667 ));

And that's all. Now your microservice will subscribe to messages published via Redis. The rest works same - patterns, error handling, etc.


Now, let's see how to create client. Previously, your client instance configuration looks like that:

@Client({ transport: Transport.TCP, port: 5667 })
client: ClientProxy;

We want to use Redis instead of TCP, so we have to change those settings:

@Client({ transport: Transport.REDIS, url: 'redis://localhost:6379' })
client: ClientProxy;

Easy, right? That's all. Other functionalities works same as in TCP communication.

results matching ""

    No results matching ""