https://github.com/iamolegga/nestjs-gcp-pubsub
The most basic and unopinionated implementation of GCP PubSub transport for NestJS microservices
https://github.com/iamolegga/nestjs-gcp-pubsub
Last synced: about 1 month ago
JSON representation
The most basic and unopinionated implementation of GCP PubSub transport for NestJS microservices
- Host: GitHub
- URL: https://github.com/iamolegga/nestjs-gcp-pubsub
- Owner: iamolegga
- License: mit
- Created: 2022-07-14T07:49:13.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-11T02:21:23.000Z (7 months ago)
- Last Synced: 2025-03-14T11:37:34.887Z (7 months ago)
- Language: TypeScript
- Homepage:
- Size: 4.38 MB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nestjs-gcp-pubsub
The most basic and unopinionated implementation of [GCP PubSub](https://cloud.google.com/pubsub/) transport for NestJS microservices.
The publisher should not care who will handle the event and by which pattern, it only knows the topic. So, no hardcoded patterns in PubSub messages: on publishing events pass the `topic-name` as a pattern, and on subscription pass `topic-name/subscription-name` string as a pattern.
`ack()` is called automatically when no errors are thrown while handling, otherwise `nack()` is called.
No topics and subscriptions are created automatically. Because we care about [security](https://cloud.google.com/pubsub/docs/authentication).
---
No request-response messaging support and it won't be added, as it's better to use appropriate RPC transports
---
## install
```sh
npm i nestjs-gcp-pubsub @google-cloud/pubsub
```## configure
### setup server:
```ts
import { GCPPubSubStrategy } from 'nestjs-gcp-pubsub';NestFactory.createMicroservice(
AppModule,
{
strategy: new GCPPubSubStrategy({
// Props of { ClientConfig } from '@google-cloud/pubsub'
projectId: 'my-project-id',// Optional deserializer, please see
// implementation in the sources.
// Default deserializer converts message's
// data (Buffer type) to string and
// parse it as JSON
deserializer: new MyDeserializer();// Optional map of subscription options
// by pattern
subscriptionOpts: {
'my-topic/my-subscription': {
// Props of { SubscriptionOptions }
// from '@google-cloud/pubsub'
batching: { maxMessages: 10 },
}
};
});
},
)
```### setup client:
```ts
import { ClientsModule } from '@nestjs/microservices';
import { GCPPubSubClient, GCPPubSubClientOptions } from 'nestjs-gcp-pubsub';export const clientToken = Symbol();
@Module({
imports: [
ClientsModule.register([
{
name: clientToken,
customClass: GCPPubSubClient,
options: {
// Optional serializer, please see
// implementation in the sources.
// Default serializer converts emitted
// data to a JSON-string and pass it
// as a Buffer to outgoing Message's
// data field.
serializer: new MySerializer(),// Optional map of topic options by
// topic-name
topicOpts: {
'my-topic': {
// Props of { PublishOptions }
// from '@google-cloud/pubsub'
messageOrdering: true,
}
},
},
},
]),
],
})
class AppModule {}
```## usage
```ts
import { ClientProxy, EventPattern } from '@nestjs/microservices';
import { GCPPubSubContext } from 'nestjs-gcp-pubsub';@Controller()
class TestController {
constructor(
// token that passed to ClientsModule.register
@Inject(clientToken)
private readonly client: ClientProxy
) {}// to get only payload you don't need decorators
@EventPattern('my-topic/my-subscription')
handle(payload: MyType) {
//
}// with context you have to use decorators
@EventPattern('my-topic/my-subscription-two')
handle(
@Payload() payload: MyType,
@Ctx() ctx: GCPPubSubContext
) {
//
}async emit() {
await this.client.emit('my-topic', data as MyType).toPromise();
}
}
```Do you use this library?
Don't be shy to give it a star! ★Also if you are into NestJS you might be interested in one of my other NestJS libs.