question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Microservices interaction with external services

See original GitHub issue

I’m submitting a…


[ ] Regression 
[X ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When using nestjs as a NATS microservice, the nats payload (data), when using the @MessagePattern decorator, is always undefined.

  @MessagePattern('sms.send')
  test(data: any) {
    console.log('function called OK');
    console.log(data); // Allways undefined
  }

In the provided example I can log “function called OK” but data is allways undefined.

Here is my main.ts

import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice(AppModule, {
    transport: Transport.NATS,
    options: {
      url: 'nats://nats:4222',
      queue: 'sms',
    },
  });
  app.listen(() => console.log('Microservice is listening'));
}
bootstrap();

I have tried both regular strings and json encoded objects as valid payloads.

Expected behavior

data should not be undefined

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: X.Y.Z

 
For Tooling issues:
- Node version: XX  
- Platform:  

Others:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
purplefish32commented, May 9, 2019

I dont think it is a documentation issue, I think is the way that Nest microservices communicate via NATS is incorrect in that a NATS payload should be by default an arbitairy string not an opiniated JSON object.

3reactions
goodoldneoncommented, Jun 17, 2019

@kamilmysliwiec

Are there any plans to allow custom shape? I’d like to migrate our services to NestJS, but we currently use this shape:

{
  type,          // Equivalent of NestJS "pattern"
  appId,         // Some data specific to our platform.
  loginUserId,   // Some data specific to our platform.
  payload,       // Equivalent of NestJS "data"
}

So far, this is really the only hurdle for our migration to NestJS. Awesome framework!

[EDIT]

Is there a way to add translation logic for messages? I tried using an Interceptor, but undefined is the only element in the context.args array. Looks like Nest is pulling out the data property before the Interceptor is called.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interservice communication in microservices - Microsoft Learn
Synchronous communication. In this pattern, a service calls an API that another service exposes, using a protocol such as HTTP or gRPC.
Read more >
Microservices Communications - Medium
Because microservices are distributed and microservices communicate with each other by inter-service communication on network level.
Read more >
Chapter 8. External API patterns - Microservices Patterns
Figure 8.9. The API gateway's API consists of a graph-based schema that's mapped to the services. A client issues a query that retrieves...
Read more >
Microservices with External Integration
Searching for micro services integrate external service gives lots of information. – Greg Burghardt. May 14, 2020 at 11:30. 2.
Read more >
Microservices External API Integration Patterns
Direct Communication It is possible to design APIs in such a way that clients can directly invoke the services. Microservice architecture.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found