7

I started working with NestJs recently and got stock while trying to test my NestJs microservices app using a TCP client

Is it possible to trigger an @EventPattern or @MessagePattern() using a non-nest app?

When trying this method the the socket client just stuck on trying to connect.

Any ideas?

Thanks.

1
  • Yes, it is possible to connect a nest app to a non-nest client but you have to pay attention to the special handling of nest, see this answer: stackoverflow.com/a/54294325/4694994 It's hard to tell what the connection issue is without seeing your setup. Can you post the relevant parts of your code? Commented Apr 11, 2019 at 9:52

1 Answer 1

12

Update Feb 2020

Since nest v6.6.0, it has become easier to integrate external services with a message de/serializer.
Have a look at the corresponding PR.


Original Answer

You have to set up the ports correctly to use it with nest:

The pattern you want to send is

<json-length>#{"pattern": <pattern-name>, "data": <your-data>[, "id": <message-id>]}

Example:

76#{"pattern":"sum","data":[0,3,3],"id":"ce51ebd3-32b1-4ae6-b7ef-e018126c4cc4"}

The parameter id is for @MessagePattern, without it @EventPattern will be triggered.

enter image description here

In your main.ts you setup the nest server. That's the port you want to send to from Packet Sender (enter at 1).

const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.TCP,
  options: { host: 'localhost', port: 3005 },
  //                            ^^^^^^^^^^
});

Then you want your nest @Client to listen to messages coming from Packet Sender (see position 2 in image)

@Client({
  transport: Transport.TCP,
  options: { host: 'localhost', port: 52079 },
  //                            ^^^^^^^^^^^  
})
private client: ClientTCP;

Then connect your client:

async onModuleInit() {
  await this.client.connect();
}

and define a @MessagePattern:

@MessagePattern('sum')
sum(data: number[]): number {
  console.log(data);
  return data.reduce((a, b) => a + b, 0);
}

As you can see, in the example I'm sending [0,3,3] and nest correctly responds with the sum 6.

Sign up to request clarification or add additional context in comments.

6 Comments

@kernkim the message structure was exactly what I needed. by creating a massage [MSG_LEN]#{ pattern: "[PATTERN_STRING]", data: "[DATA]" } I'm able to send custom messages. Thenks
I have checked this - it is exactly what I want it to do , but I can't get it to work. HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n . I am using this sample github.com/nestjs/nest/tree/master/sample/03-microservices - which seems to work for internal communication - but I can't connect with the service through packet sender.
@MartinThompson I don't have time to look into this atm, but there have been changes that should make it easier to integrate external services. Have a look at this pr: github.com/nestjs/nest/pull/2653 If your problem persists, maybe open a new question so someone can have a look at it. :)
Thanks @KimKern . It was actually easier than I thought. I just wasn't putting the correct pattern in:
It is: 89#{"pattern": { "cmd": "sum" },"data":[0,3,3],"id":"ce51ebd3-32b1-4ae6-b7ef-e018126c4cc4"}
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.