Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Custom Google Cloud Pub/Sub microservice transport for Nest framework (node.js)

License

NotificationsYou must be signed in to change notification settings

p-fedyukovich/nestjs-google-pubsub-microservice

Repository files navigation

Nest Logo

Google Cloud Pub/Sub

Pub/Sub is an asynchronous messaging service that decouples services that produce events from services that process events.

You can use Pub/Sub as messaging-oriented middleware or event ingestion and delivery for streaming analytics pipelines.

Pub/Sub offers durable message storage and real-time message delivery with high availability and consistent performance at scale

Installation

To start building Pub/Sub-based microservices, first install the required packages:

$ npm i --save @google-cloud/pubsub nestjs-google-pubsub-microservice

Overview

To use the Pub/Sub transporter, pass the following options object to thecreateMicroservice() method:

constapp=awaitNestFactory.createMicroservice<MicroserviceOptions>(ApplicationModule,{strategy:newGCPubSubServer({topic:'cats_topic',subscription:'cats_subscription',client:{projectId:'microservice',},}),},);

Options

Theoptions property is specific to the chosen transporter. TheGCloud Pub/Sub transporter exposes the properties described below.

topicTopic name which your server subscription will belong to
subscriptionSubscription name which your server will listen to
replyTopicTopic name which your client subscription will belong to
replySubscriptionSubscription name which your client will listen to
noAckIffalse, manual acknowledgment mode enabled
initIffalse, topics and subscriptions will not be created, only validated
checkExistenceIffalse, topics and subscriptions will not be checked, only used. This only applies wheninit isfalse
useAttributesOnly applicable for client. Iftrue,pattern andcorrelationId will be sent via message attributes. This is useful if message consumer is not NestJs microservice or you have message filtering on subscription
clientAdditional client options (read morehere)
publisherAdditional topic publisher options (read morehere)
subscriberAdditional subscriber options (read morehere)
scopedEnvKeyScope topics and subscriptions to avoid losing messages when several people are working on the same code base. Will prefixes topics and subscriptions with this key (read morehere)

Client

constclient=newGCPubSubClient({client:{apiEndpoint:'localhost:8681',projectId:'microservice',},});client.send('pattern','Hello world!').subscribe((response)=>console.log(response));

Context

In more sophisticated scenarios, you may want to access more information about the incoming request. When using the Pub/Sub transporter, you can access theGCPubSubContext object.

@MessagePattern('notifications')getNotifications(@Payload()data:number[], @Ctx()context:GCPubSubContext){console.log(`Pattern:${context.getPattern()}`);}

To access the original Pub/Sub message (with theattributes,data,ack andnack), use thegetMessage() method of theGCPubSubContext object, as follows:

@MessagePattern('notifications')getNotifications(@Payload()data:number[], @Ctx()context:GCPubSubContext){console.log(context.getMessage());}

Message acknowledgement

To make sure a message is never lost, Pub/Sub supportsmessage acknowledgements. An acknowledgement is sent back by the consumer to tell Pub/Sub that a particular message has been received, processed and that Pub/Sub is free to delete it. If a consumer dies (its subscription is closed, connection is closed, or TCP connection is lost) without sending an ack, Pub/Sub will understand that a message wasn't processed fully and will re-deliver it.

To enable manual acknowledgment mode, set thenoAck property tofalse:

{replyTopic:'cats_topic_reply',replySubscription:'cats_subscription_reply',noAck:false,client:{projectId:'microservice',},},

When manual consumer acknowledgements are turned on, we must send a proper acknowledgement from the worker to signal that we are done with a task.

@MessagePattern('notifications')getNotifications(@Payload()data:number[], @Ctx()context:GCPubSubContext){constoriginalMsg=context.getMessage();originalMsg.ack();}

Shutdown

Pub/Sub requires a graceful shutdown properly configured in order to work correctly, otherwise some messages acknowledges can be lost. Therefore, don't forget to call client close:

exportclassGCPubSubControllerimplementsOnApplicationShutdown{client:ClientProxy;constructor(){this.client=newGCPubSubClient({});}onApplicationShutdown(){returnthis.client.close();}}

About

Custom Google Cloud Pub/Sub microservice transport for Nest framework (node.js)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors7


[8]ページ先頭

©2009-2025 Movatter.jp