nats-io / nats.js

Apache License 2.0
31 stars 4 forks source link

Nats request/Reply Nest.js problem with await in TS #66

Open FuriousGopher opened 2 days ago

FuriousGopher commented 2 days ago

Observed behavior

I'm tyrying to use Nats and specific client.send to get return some data from sended message. I start nats service in my Nats module

{ provide: 'Nats', useFactory: (): ClientProxy => { return ClientProxyFactory.create({ transport: Transport.NATS, options: { servers: [ nats://${process.env.NATS_HOST}:${process.env.NATS_PORT}, ], }, }); }, },

and i made nats.service 

` import { Injectable, Inject, OnModuleInit, OnModuleDestroy, } from '@nestjs/common'; import { ClientProxy } from '@nestjs/microservices'; import { lastValueFrom } from 'rxjs';

@Injectable() export class NatsService implements OnModuleInit, OnModuleDestroy { constructor(@Inject('Nats') private readonly client: ClientProxy) {}

async onModuleInit() { try { await this.client.connect(); } catch (error) { console.error('Error connecting to NATS:', error); throw error; } }

async onModuleDestroy() { try { await this.client.close(); } catch (error) { console.error('Error closing NATS connection:', error); } }

emitEvent(pattern: string, payload: any) { return this.client.emit(pattern, payload); }

async execute(pattern: string, payload: any) { try { const response = await lastValueFrom(this.client.send(pattern, payload)); console.log(Received response for pattern "${pattern}":, response); return response; } catch (error) { console.error( Error sending event with pattern "${pattern}":, error.message, ); console.error(Full error:, error); throw error; } } } `

Trying to test like this

@Post('/private/join') async privateJoin() { // @CurrentUser() currentUser: CurrentUserEntity, const user = 'cm1d7c7590048w3puv6d9rs4o'; const g = await this.natsService.execute(NatsRoomEvent.JOIN_ROOM, { userId: user, }); console.log(" @Post('/private/join')", g); return g; }

and receiving event like this

`@MessagePattern(NatsRoomEvent.JOIN_ROOM) async handleNatsMessage(@Payload() data: any): Promise { console.log('Received message:', data);

// Simulate delay
await new Promise((resolve) => setTimeout(resolve, 1000));

return { status: 'success', message: `Processed user ${data.userId}` };

}`

and i console i see this error

Received message: { userId: 'cm1d7c7590048w3puv6d9rs4o' } Error sending event with pattern "joinRoom.event": Internal server error Full error: { status: 'error', message: 'Internal server error' } [Nest] 115228 - 09/27/2024, 5:45:32 PM ERROR [ExceptionsHandler] Internal server error

Expected behavior

But after i comment await everything is working fine

[Nest] 117088 - 09/27/2024, 5:48:34 PM LOG [NestApplication] Nest application successfully started +1504ms Received message: { userId: 'cm1d7c7590048w3puv6d9rs4o' } MessagePattern with delay completed: 200 Received response for pattern "joinRoom.event": 200 @Post('/private/join') 200

Server and client version

Last Nats Helm chart version

Host environment

No response

Steps to reproduce

Maybe i install Nats by helm somehow incorrect. Because when i use just docker compose with helm everything is forking with await and not. Sorry if make a lot of nonsense just spend for this 15 h

aricart commented 2 days ago

@FuriousGopher this looks like it is a helm chart thing right? - this is not a npm nats problem - can you print the version (I think you have the wrong repo)

aricart commented 2 days ago

(can you show where you import connect