p-fedyukovich / nestjs-google-pubsub-microservice

Custom Google Cloud Pub/Sub microservice transport for Nest framework (node.js)
https://nestjs.com
MIT License
86 stars 14 forks source link

There is no matching event handler defined in the remote service. Event pattern: undefined error message #11

Closed RezaRahmati closed 2 years ago

RezaRahmati commented 2 years ago

Hi I tried to use the library but I keep getting "There is no matching event handler defined in the remote service. Event pattern: undefined" error when a new message arrives.

my code is like this.

main.ts

async function bootstrap() {
    const port = process.env.PORT || 3000;

    const configApp = await NestFactory.create(AppModule);
    const configService = configApp.get(ConfigService);
    const loggerService = new LoggerService();

    const app = await NestFactory.create(AppModule, {
        logger: loggerService
    });

    const microservice2 = app.connectMicroservice({
        strategy: new GCPubSubServer({
            topic: 'test-latency',
            subscription: 'test-latency-pull',
            noAck: true,
            client: {
                projectId: 'cmor-baas-dev'
            }
        })
    });

    await app.startAllMicroservices();
    microservice2.useLogger(loggerService);

    await app.listen(port);
}
bootstrap();

Then in app.controller

    @MessagePattern('notifications')
    getNotifications(@Payload() data: any, @Ctx() context: GCPubSubContext) {
        console.log(`Pattern: ${context.getPattern()}`);
        console.log(context.getMessage());
        console.log(JSON.stringify(data));
    }

image

nestjs:8.2.5 (I tried with 8.0.0 as well) @google-cloud/pubsub: 2.19.4 nestjs-google-pubsub-microservice: 2.2.0

p-fedyukovich commented 2 years ago

Hi, could you provide pubsub message example which client sends?

RezaRahmati commented 2 years ago

@p-fedyukovich Sure

message example

{
  "broker": "Smart Prime Group Ltd.",
  "time": "2022-02-14T00:21:01.137Z",
  "symbol": "SPA35",
  "ask": 50.25,
  "bid": 60.50,
  "volume": 6500,
  "publisherTime": "2022-02-14T00:21:01.137Z"
}

here is my topic schema

{
  "type": "record",
  "name": "Avro",
  "fields": [
    {
      "name": "broker",
      "type": "string"
    },
    {
      "name": "time",
      "type": "string"
    },
    {
      "name": "symbol",
      "type": "string"
    },
    {
      "name": "ask",
      "type": "double"
    },
    {
      "name": "bid",
      "type": "double"
    },
    {
      "name": "volume",
      "type": "double"
    },
    {
      "name": "publisherTime",
      "type": "string"
    }
  ]
}

p.s I tried with this lib and it's working fine https://github.com/crallen/nestjs-google-pubsub#readme

p-fedyukovich commented 2 years ago

@p-fedyukovich Sure

message example

{
  "broker": "Smart Prime Group Ltd.",
  "time": "2022-02-14T00:21:01.137Z",
  "symbol": "SPA35",
  "ask": 50.25,
  "bid": 60.50,
  "volume": 6500,
  "publisherTime": "2022-02-14T00:21:01.137Z"
}

here is my topic schema

{
  "type": "record",
  "name": "Avro",
  "fields": [
    {
      "name": "broker",
      "type": "string"
    },
    {
      "name": "time",
      "type": "string"
    },
    {
      "name": "symbol",
      "type": "string"
    },
    {
      "name": "ask",
      "type": "double"
    },
    {
      "name": "bid",
      "type": "double"
    },
    {
      "name": "volume",
      "type": "double"
    },
    {
      "name": "publisherTime",
      "type": "string"
    }
  ]
}

p.s I tried with this lib and it's working fine https://github.com/crallen/nestjs-google-pubsub#readme

Your message data format is wrong because it should have metadata for correct selecting message handler. If you use your own message dispatcher it should produce message in such format

{
  "pattern": "notifications",
  "eventId": "any string value for that message which keeps idemopotency",
  "data": "Your message payload"
}
RezaRahmati commented 2 years ago

Thank you for reply. So what changes exactly I need to make in my message and schema to make it work?

p-fedyukovich commented 2 years ago

I provided the message structure, you should update your message producer to fit the message structure

KristianPu commented 1 year ago

Hi @p-fedyukovich I have trouble publishing the message, could you please provide a working example here or to the ReadMe file?

p-fedyukovich commented 1 year ago

Hi @p-fedyukovich I have trouble publishing the message, could you please provide a working example here or to the ReadMe file?

Hi, there are examples in test files.

import {
  Body,
  Controller,
  HttpCode,
  OnApplicationShutdown,
  Post,
  Query,
} from '@nestjs/common';
import {
  ClientProxy,
  EventPattern,
  MessagePattern,
} from '@nestjs/microservices';
import { GCPubSubClient } from '../../lib';

@Controller()
export class GCPubSubController implements OnApplicationShutdown {
  client: ClientProxy;

  constructor() {
    this.client = new GCPubSubClient({
      client: {
        apiEndpoint: 'localhost:8681',
        projectId: 'microservice',
      },
    });
  }

  onApplicationShutdown(signal?: string) {
    return this.client.close();
  }

  @Post('notify')
  async sendNotification(): Promise<any> {
    return this.client.emit('notification', true);
  }
}
vladimirovsk commented 8 months ago

Привет @p-fedyukovichУ меня возникли проблемы с публикацией сообщения. Не могли бы вы предоставить рабочий пример здесь или в файле ReadMe?

Привет, в тестовых файлах есть примеры.

import {
  Body,
  Controller,
  HttpCode,
  OnApplicationShutdown,
  Post,
  Query,
} from '@nestjs/common';
import {
  ClientProxy,
  EventPattern,
  MessagePattern,
} from '@nestjs/microservices';
import { GCPubSubClient } from '../../lib';

@Controller()
export class GCPubSubController implements OnApplicationShutdown {
  client: ClientProxy;

  constructor() {
    this.client = new GCPubSubClient({
      client: {
        apiEndpoint: 'localhost:8681',
        projectId: 'microservice',
      },
    });
  }

  onApplicationShutdown(signal?: string) {
    return this.client.close();
  }

  @Post('notify')
  async sendNotification(): Promise<any> {
    return this.client.emit('notification', true);
  }
}

What is apiEndpoint: 'localhost:8681' responsible for, how to use it?

p-fedyukovich commented 8 months ago

What is apiEndpoint: 'localhost:8681' responsible for, how to use it?

This is yours pubsub server address

vladimirovsk commented 8 months ago

What is apiEndpoint: 'localhost:8681' responsible for, how to use it?

This is yours pubsub server address

From the example above, it's not quite clear where I'm creating a microservice with this port.

I would be very grateful if you could tell me where I can see a working example of server and client operation. It seems to me that these examples do not quite explain everything