#Microservices - workers / RabbitMQ

13 messages Β· Page 1 of 1 (latest)

unkempt whale
#

Hello!

Thank you for reading this post. I'm Symfony PHP developer but I want to migrate to NodeJS/NestJS framework. I can see some nice solution for "workers" as background processes.

Can someone confirm my understending how the "Microservices" concept works in NestJS?

So let's say I have "some application". I want to publish a message to RabbitMQ via Client

ClientsModule.register([
      {
        name: 'MATH_SERVICE',
        transport: Transport.RMQ,
        options: {
          urls: ['amqp://localhost:5672'],
          queue: 'cats_queue',
          queueOptions: {
            durable: false
          },
        },
      },
    ]),

My question is:

  • How to publish a "message" to "exchange"?
  • How to define "exchange" for RabbitMQ via "Microservices/Client" - is it possible?
  • Is it possible to listen on more then 1 queue in worker? I mean to have a 1 process which listen on multiple queues?
  • I think NestJS doesn't have any component/lib with real messenging implementation?
astral ridge
#

Try rereading the docs again.

https://docs.nestjs.com/microservices/basics
https://docs.nestjs.com/microservices/rabbitmq

It's all there.

But, to answer your questions....

  1. Via the client proxy. From the example in the docs:
    as a request/response process
accumulate(): Observable<number> {
  const pattern = { cmd: 'sum' };
  const payload = [1, 2, 3];
  return this.client.send<number>(pattern, payload);
}

and as an event

async publish() {
  this.client.emit<number>('user_created', new UserCreatedEvent());
}
  1. CRUDing Exchanges is an administrative process. You wouldn't want clients able to make or alter exchanges.
#
  1. You can create multiple clients where each can listen to its own queue.
#
  1. What is a "real messaging implementation"?
unkempt whale
#

@astral ridge I really appreciate your answers πŸ™‚ Thank you 🌹

  1. So I can assume "CRUDing Exchanges is an administrative process" - I can write some kind of migration by native amqplib ? As I understood ClientModule - canno't manage exchanges - (only queues from consumer side πŸ€” )?

  2. βœ… If I understood correctly in modular monoloth. When I would like run a worker. I need to create separate file like worker.ts with some example implementation:

import { NestFactory } from '@nestjs/core';
import { EventModule } from './shared/Components/Event/event.module';
import { Transport } from '@nestjs/microservices';

async function bootstrap() {
  const app = await NestFactory.createMicroservice(EventModule, {
    transport: Transport.RMQ,
    options: {
      urls: ['amqp://guest:guest@localhost:5672'],
      queue: 'event_queue',
      queueOptions: {
        durable: true,
      },
      prefetchCount: 1,
    },
  });

  await app.listen();
}
bootstrap();
  1. I apologize for this sentence "real messaging implementation" - maybe is exists but some work must be done.

Let me elaborate a bit more. So it's really simillar what we have in @nestjs/cqrs. It means to have possibility for creating some "buses", "management transport layer", "serialization process". Core concept is push a message and handle message πŸ™‚ But with abstraction transport layer (which doesn't exists in @nestjs/cqrs).

I know it is a Symfony example, but I think the documentation is really simple to see how it works πŸ™‚
https://symfony.com/doc/current/messenger.html

Also Java have similar in SPRING.

astral ridge
#
  1. The Microservices Client module can't manage anything. It's just a simple client for listening, and emitting or sending messages to a single queue. If you want your app to also do admin tasks to the broker, then you'd need to find another way to access the broker. The Nest client won't help.

  2. Microservices have absolutely nothing to do with workers. If you are trying to build up a bus (messaging) system to run or rather communicate between workers and your app, you'd need to just use the events module. https://docs.nestjs.com/techniques/events

  3. So, CQRS is a tool for managing three things in the microservices world.

  • request pipelines i.e. querying for data
  • modification pipeline i.e. for "commanding" that things/ state/data/etc. change
  • (in a proper implementation) distributed transactions i.e. control that a process completes successfully across mulitple microservices

Are you wanting to work with microservices or do you just need internal workers to split out work for your application?

unkempt whale
#

Thank you. I have communication based on rabbitmq between 5 microservices & using protobuf as our serialization tool.

So probably Microservices

astral ridge
#

@unkempt whale - Yeah, so microservices in Nest are their own applications and wouldn't work as nodejs worker threads. The microservices would usually be on separate servers.

#

Or, they might be inside pods in k8s.

#

Or in containers in a Docker setup.

unkempt whale
#

Thank you, I think we can close thread πŸ™‚

unkempt whale
#

@astral ridge 1 more thing for elaboration "It's just a simple client for listening, and emitting or sending messages to a single queue"

So it is not possible to push message on exchange . Not directly to queue?

astral ridge
#

@unkempt whale - I'm not an expert on microservices with Nest or with Rabbit. But, Nest uses the AMQP client under the hood, so you should be able to get your answers there.