@gus-eip/platform-listener
is a NestJS service for subscribing to Salesforce platform events using the CometD protocol. It handles authentication with Salesforce, establishes a connection to the CometD server, and provides an observable stream of received events.
To install this package, use npm:
npm install @gus-eip/platform-listener rxjs
First, you need to import the PlatformEventService
into your NestJS module.
import { Module } from '@nestjs/common';
import { PlatformEventService } from '@gus-eip/platform-listener';
@Module({
providers: [PlatformEventService],
exports: [PlatformEventService],
})
export class AppModule {}
Inject the PlatformEventService
into your controller or another service where you want to consume the events.
import { Controller, OnModuleInit } from '@nestjs/common';
import { PlatformEventService } from '@gus-eip/platform-listener';
@Controller()
export class AppController implements OnModuleInit {
constructor(private readonly platformEventService: PlatformEventService) {}
async onModuleInit() {
// Initialize the platform listener
await this.platformEventService.platformListener('YourPlatformEventName');
// Subscribe to the event stream
this.platformEventService.getEventStream().subscribe((message) => {
console.log('Received event:', message);
// Process the event here
});
}
}
When creating an instance of the PlatformEventService
, you need to provide the necessary Salesforce credentials and configuration details.
import { PlatformEventService } from '@gus-eip/platform-listener';
const platformEventService = new PlatformEventService(
'your-username',
'your-password',
'your-client-id',
'your-client-secret',
'your-auth-url',
'your-event-table'
);
Here's a full example:
import { Module } from '@nestjs/common';
import { PlatformEventService } from '@gus-eip/platform-listener';
import { AppController } from './app.controller';
@Module({
providers: [
{
provide: PlatformEventService,
useFactory: () => {
return new PlatformEventService(
'your-username',
'your-password',
'your-client-id',
'your-client-secret',
'your-auth-url',
'your-event-table'
);
},
},
],
controllers: [AppController],
exports: [PlatformEventService],
})
export class AppModule {}
import { Controller, OnModuleInit } from '@nestjs/common';
import { PlatformEventService } from '@gus-eip/platform-listener';
@Controller()
export class AppController implements OnModuleInit {
constructor(private readonly platformEventService: PlatformEventService) {}
async onModuleInit() {
await this.platformEventService.platformListener('YourPlatformEventName');
this.platformEventService.getEventStream().subscribe((message) => {
console.log('Received event:', message);
// Process the event here
});
}
}
Initializes the platform event listener for the specified event name.
-
platformEventName
: The name of the Salesforce platform event to subscribe to.
Returns an observable stream of received events. Other parts of the application can subscribe to this observable to receive real-time updates.
MIT
This `README.md` file provides a clear guide on how to install, configure, and use the `@gus-eip/platform-listener` package in a NestJS application. It includes detailed instructions, example code, and API documentation.