【问题标题】:Nest js read events from rabbitmq in websockets microserviceNest js从websockets微服务中的rabbitmq读取事件
【发布时间】:2022-03-30 15:25:54
【问题描述】:

我创建了一个带有rabbitmq 连接的微服务(通知微服务)。我从那里有一个产品微服务,我将向 rabbitmq 推送一个事件,以通知客户有关新产品的信息。在 notify-microservice 微服务中,我有一个 websocket。我如何从 rabbitmq 读取事件以通知客户。 现在我只能从微服务控制器中读取来自 rabbitmq 的事件。我如何从 websocket 文件中读取数据。

微服务 main.ts 文件:

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


const logger = new Logger('Main');
const microserviceOptions = {
  transport: Transport.RMQ,
  options: {
    urls: ['amqp://<user_name>:<password>@<host>:<port>/<vhost>'],
    queue: '<queue_name>',
    queueOptions: {
      durable: false
    },
  }
}

async function bootstrap() {
  const app = await NestFactory.createMicroservice(AppModule, microserviceOptions);
  app.listen(() => {
    logger.log("Socket micro service is listening...");
  })
}
bootstrap();

app.gateway.ts 文件

import { Logger } from '@nestjs/common';
import { OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit, SubscribeMessage, WebSocketGateway, WsResponse } from '@nestjs/websockets';
import { Socket,Server } from 'socket.io';

import { EventPattern } from '@nestjs/microservices';

@WebSocketGateway(3001)

export class AppGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect{
    private Logger = new Logger('AppGateway');

    afterInit(server: Server) {
        this.Logger.log("App Gateway Initialized");
    }

    handleConnection(client: Socket, ...args: any[]){
        this.Logger.log(`New client connected...: ${client.id}`);
        client.emit('connected', 'Successfully connected to the server.');
    }

    handleDisconnect(client: Socket) {
        this.Logger.log(`Client disconnected: ${client.id}`);
    }


    @EventPattern('notify_new_product')
    notifyNewProduct(client:Socket, text:string):WsResponse<string> {
        this.Logger.log(`got new event`);
        return {event: 'notify_new_product', 'data': text};
    }
}

app.module.ts:

import { Module } from '@nestjs/common';
import { AppGateway } from './app.gateway';
import { NotifyController } from './notify.controller';
import { ClientsModule, Transport } from '@nestjs/microservices';

@Module({
  imports: [],
  controllers: [NotifyController],
  providers: [AppGateway],
})
export class AppModule {}

notify.controller.ts

import { Body, Controller, Get, Logger, Post } from '@nestjs/common';
import { EventPattern } from '@nestjs/microservices';

@Controller()
export class NotifyController {

  private logger = new Logger('NotifyController');

  @EventPattern('notify_new_product')
  async notify_new_product(product_id: string) {
    
    this.logger.log('Received new event to notify');
  }
}

【问题讨论】:

    标签: websocket nestjs


    【解决方案1】:

    消息模式装饰器只能在控制器类中使用,因为它们是应用程序的入口点。在提供程序中使用它们不会产生任何影响,因为它们会被 Nest 忽略。

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      • 2021-01-12
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      相关资源
      最近更新 更多