Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RezaRahmati opened this issue May 11, 2022 · 10 comments

Comments

@RezaRahmati
Copy link

RezaRahmati commented May 11, 2022

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
Copy link
Owner

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

@RezaRahmati
Copy link
Author

@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
Copy link
Owner

@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
Copy link
Author

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

@p-fedyukovich
Copy link
Owner

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

@KristianPu
Copy link

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

@p-fedyukovich
Copy link
Owner

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
Copy link

Привет @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
Copy link
Owner

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

This is yours pubsub server address

@vladimirovsk
Copy link

vladimirovsk commented Feb 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants