Libreria di Google PubSub per NestJs
Questa repository contiene il publisher e il subscriber per i messaggi di Google PubSub.
Client
E' possibile utilizzare il client importando il modulo PubSubClientCoreModule
Esempio di utilizzo
import { Module } from '@nestjs/common'
import { ConfigModule, PubSubClientConfig } from './config'
@Module({
imports: [
ConfigModule,
PubSubClientCoreModule.forRootAsync({
imports: [ConfigModule],
useClass: PubSubClientConfig,
}),
],
controllers: [],
providers: [],
})
export class AppModule {}
Server
E' possibile utilizzare il server per rimanere in ascolto sui topic utilizzando la funzione makeTransportStrategy
.
Esempio di utilizzo
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { NestExpressApplication } from '@nestjs/platform-express'
import { ClientConfig } from '@google-cloud/pubsub'
import { ConfigService } from './config'
import { makeTransportStrategy } from '@advision/google-pubsub-nestjs'
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule)
const configService = app.get(ConfigService)
const options: ClientConfig = {}
if (configService.get('PUBSUB_NAMESPACE') === 'Dev') {
options['projectId'] = configService.get('PUBSUB_EMULATOR_PROJECT')
options['apiEndpoint'] = configService.get('PUBSUB_EMULATOR_HOST')
} else {
options['keyFilename'] = configService.get(
'PUBSUB_SERVICE_ACCOUNT_PATH'
)
}
app.connectMicroservice({
strategy: makeTransportStrategy({
prefixName: configService.get('PUBSUB_NAMESPACE'),
clientConfig: options,
}),
})
app.startAllMicroservices()
return app.listen(3000, '0.0.0.0')
}
bootstrap()