Transport & Patterns — Series 3

Preview — 3 of 10 questions

Why the async form rather than register?

javascript
ClientsModule.registerAsync([
  {
    name: 'ORDERS_SERVICE',
    imports: [ConfigModule],
    inject: [ConfigService],
    useFactory: (c: ConfigService) => ({
      transport: Transport.RMQ,
      options: { urls: [c.getOrThrow<string>('RABBITMQ_URL')], queue: 'orders' },
    }),
  },
]);
ABecause register cannot configure the RabbitMQ transport
BThe options come from a factory resolved through the container, so the broker URL is read from validated configuration rather than captured from process.env while the module's metadata was first evaluated — which happens before any .env file has been loaded
CBecause the async form connects lazily and the synchronous one connects eagerly
DBecause only the async form allows more than one client per module

What does @Ctx() provide?

javascript
@EventPattern('order.created')
handle(@Payload() data: OrderCreated, @Ctx() context: RmqContext) {}
AThe Nest execution context, with getHandler() and getClass()
BThe application's configuration for the current transport
CA per-message request-scoped store
DThe transport-specific context object — for RabbitMQ the channel and the original message, for Kafka the topic, partition and offset — which is what manual acknowledgement, offset commits and header access all need

Does this match?

javascript
// producer
client.send({ cmd: 'sum', version: 1 }, [1, 2, 3]);
// consumer
@MessagePattern({ cmd: 'sum' })
sum(@Payload() data: number[]) {}
ANo — object patterns are compared by their serialised form, so { cmd: 'sum', version: 1 } and { cmd: 'sum' } are different patterns and no handler matches
BYes, because cmd matches and extra keys are ignored
CYes, because object patterns are matched by partial containment
DNo, because object patterns are unsupported — only strings may be used

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.