question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Event not handled by event store.

See original GitHub issue

As soon as I implement the “nestjs-event-store” like in your branche “next” in ‘ultimate-backend’ the handler is not called + the event is not received in the event store

app.module.ts

@Module({
  imports: [
    ProductModule,
    EventStoreModule.register({
      tcpEndpoint: {
        host: 'http://localhost',
        port: 1113,
      },
      options: {
        defaultUserCredentials: {
          username: 'admin'
          password: 'changeit',
',
        },
      },
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule { }

product.module.ts

const productEventHandlers = [
  ProductCreatedHandler
];

@Module({
  imports: [
    CqrsModule,
    EventStoreModule.registerFeature({
      subscriptions: [
        {
          type: EventStoreSubscriptionType.CatchUp,
          stream: '$ce-product',
          resolveLinkTos: true, // Default is true (Optional)
          lastCheckpoint: 13, // Default is 0 (Optional)
        },
        {
          type: EventStoreSubscriptionType.Volatile,
          stream: '$ce-product',
        },
        {
          type: EventStoreSubscriptionType.Persistent,
          stream: '$ce-product',
          persistentSubscriptionName: 'steamName',
        },
      ],
      eventHandlers: {
        ProductCreatedEvent: (id, data) => new ProductCreatedEvent(id, data),
      },
    }),
  ],
  providers: [
    ProductService,
    ...commandHandler,
    ...queryHandler,
    ...productEventHandlers,
  ],
  controllers: [],
  exports: [ProductService]
})
export class ProductModule { }

product.handler.command.create.ts

import { CommandHandler, ICommandHandler, EventBus } from "@nestjs/cqrs";

@CommandHandler(CreateProductCommand)
export class CreateProductCommandHandler implements ICommandHandler<CreateProductCommand> {

  constructor(private readonly eventBus: EventBus) { }

  async execute(command: CreateProductCommand) {
    const { dto } = command;
    this.eventBus.publish(new ProductCreatedEvent("1", dto));
    return {};
  }

}

product-created-event.ts

import { IEvent } from '@nestjs/cqrs';

export class ProductCreatedEvent implements IEvent {
    constructor(
        public _id: string,
        public readonly data: any
    )  { }
}

product-created.handler.ts

import { IEventHandler, EventsHandler } from '@nestjs/cqrs';

@EventsHandler(ProductCreatedEvent)
export class ProductCreatedHandler
  implements IEventHandler<ProductCreatedEvent> {
  handle(event: ProductCreatedEvent) {
    Logger.log(event, 'ProductCreatedEvent'); // write here
  }
}

Do I miss something or is this a bug?

Nice job btw!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
hbthegreatcommented, May 6, 2020

Hello fellow devs!

I came across this issue while running into it myself. After following these steps I am still seeing the same results. I feel like I must be missing something small and didn’t think it was worth creating another issue for it as it is probably related.

Firing up it seems to be able to connect and subscribe. image

AppModule.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AssetController } from './asset/controllers/assets.controller';
import { AssetModule } from './asset/asset.module';
import { AppService } from './app.service';
import { EventStoreModule } from '@juicycleff/nestjs-event-store';
import { RethinkModule } from './document-store/rethink.module';
@Module({
  imports: [
    RethinkModule,
    EventStoreModule.register({
      tcpEndpoint: {
        host: 'http://localhost', // process.env.ES_TCP_HOSTNAME || AppConfig.eventstore?.hostname,
        port: 1113,//parseInt(process.env.ES_TCP_PORT, 10) || AppConfig.eventstore?.tcpPort,
      },
      options: {
        maxRetries: 1000, // Optional
        maxReconnections: 1000,  // Optional
        reconnectionDelay: 1000,  // Optional
        heartbeatInterval: 1000,  // Optional
        heartbeatTimeout: 1000,  // Optional
        defaultUserCredentials: {
          password: 'admin', //AppConfig.eventstore?.tcpPassword,
          username: 'changeit' //AppConfig.eventstore?.tcpUsername,
        },
      },
    }),
    AssetModule
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

asset.module.ts

import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { EventStoreSubscriptionType, EventStoreModule } from '@juicycleff/nestjs-event-store';
import { QueryHandlers } from './queries/handlers';
import { EventHandlers } from './events/handlers';
import { CommandHandlers } from './commands/handlers';
import { AssetCreatedEvent } from './events/impl/asset-created.event';
import { AssetSagas } from './sagas/asset.sagas';
import { AssetUpdatedEvent } from './events/impl/asset-updated.event';
import { AssetDeletedEvent } from './events/impl/asset-deleted.event';
import { AssetController } from './controllers/assets.controller';
import { AssetService } from './services/asset.service';
import { AssetRepository } from './repository/asset.repository';
import { RethinkModule } from '../document-store/rethink.module';

@Module({
    imports: [
        RethinkModule,
        CqrsModule,
        EventStoreModule.registerFeature({
            featureStreamName :'$ce-asset',
            subscriptions: [
                {
                    type: EventStoreSubscriptionType.CatchUp,
                    stream: '$ce-asset'
                }
            ],
            eventHandlers: {
                AssetCreatedEvent: (data) => new AssetCreatedEvent(data),
                AssetUpdatedEvent: (data) => new AssetUpdatedEvent(data),
                AssetDeletedEvent: (data) => new AssetDeletedEvent(data)
            }
        })
    ],
    controllers: [AssetController],
    providers: [
        AssetService,
        AssetSagas,
        AssetRepository,
        ...QueryHandlers,
        ...CommandHandlers,
        ...EventHandlers,
    ],
    exports: [
        AssetService
    ]
})

export class AssetModule {}

Hitting this handler

import { ICommandHandler, CommandHandler, EventBus } from '@nestjs/cqrs';
import { CreateAssetCommand } from '../impl/create-asset.command';
import { Logger } from '@nestjs/common';
import { AssetCreatedEvent } from '../../events/impl/asset-created.event';

@CommandHandler(CreateAssetCommand)
export class CreateAssetHandler implements ICommandHandler<CreateAssetCommand> {
  constructor(
    private readonly eventBus: EventBus
  ) {}

  async execute(command: CreateAssetCommand) {
    Logger.log('Async CreateAssetHandler...', 'CreateAssetCommand');

    const { assetDto } = command;
    console.log(assetDto);
    this.eventBus.publish(new AssetCreatedEvent(assetDto));
    return {};
  }
}

image

asset-created.event.ts

import { IEvent } from '@nestjs/cqrs';

export class AssetCreatedEvent implements IEvent {
  constructor(
    public readonly asset: any
  ) {}
}

Then after some time I also run into EventStore closed! image

I don’t see any of the events getting published into eventstore either.

Any ideas?

Thanks for all your hard work I have learnt a lot from your ultimate backend and this project 😃

1reaction
hbthegreatcommented, May 11, 2020

@juicycleff would it be better to make my request a different post? Happy to if you would prefer. Still haven’t found a resolution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dealing with failed event processing - EventStoreDB
Stop processing any events until the problem is fixed. Could be a long pause if the fix is not straightforward.
Read more >
architecture - When is an event pushed to the event store in ES?
In event sourcing, the basic approach is that only valid events ever get saved ... The UI does not send events to the...
Read more >
Event handlers - Subscribing to events - Rails Event Store
Event handlers - Subscribing to events. To subscribe a handler to events in Rails Event Store you need to use #subscribe method on...
Read more >
Event Sourcing pattern - Azure Architecture Center
The event store typically publishes these events so that consumers can be notified and can handle them if needed. Consumers could, for example,...
Read more >
Event Store - dolittle.io
Events that came over the Event Horizon need to be put into a scoped collection so they won't be mixed with the other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found