1
votes

I want to use a Cloud Function for pulling messages from pub/sub. So I set up a pull-subscription to a topic. Then I trigger the Cloud Function with Cloud Scheduler every hour.

  1. Is there a better approach if I need to use pull?

I've copied an example from documentation, but it throws an error.

 TypeError: match error: could not instantiate a path template from 
 projects/projectname/subscriptions/projects/projectname/subscriptions/test at PathTemplate.match 
 (/srv/node_modules/google-gax/build/src/pathTemplate.js:82:19) at PathTemplate.render 
 (/srv/node_modules/google-gax/build/src/pathTemplate.js:118:14) at SubscriberClient.subscriptionPath 
 (/srv/node_modules/@google-cloud/pubsub/build/src/v1/subscriber_client.js:1838:57) at 
 exports.helloPubSub (/srv/index.js:22:45) at /worker/worker.js:825:24 at <anonymous> at 
 process._tickDomainCallback (internal/process/next_tick.js:229:7)
  1. What am I missing that's causing ther error?

My copied code looks like this:

const pubsub = require('@google-cloud/pubsub');
const subClient = new pubsub.v1.SubscriberClient();

const subscriptionName = 'name_of_my_subscription';
const projectId = 'my_project_id';
const timeout = 60;



exports.helloPubSub = async (event, context) => {

const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);

const request = {
  subscription: formattedSubscription,
  maxMessages: 10,
};

const [response] = await subClient.pull(request);

const ackIds = [];
for (const message of response.receivedMessages) {
  console.log(`Received message: ${message.message.data}`);
  ackIds.push(message.ackId);
}

 const ackRequest = {
 subscription: formattedSubscription,
 ackIds: ackIds,
};

await subClient.acknowledge(ackRequest);

};

Thanks!

1
I think you can use CloudFunctions to have the function directly triggered by a Pub/Sub topic publish ... see ... cloud.google.com/functions/docs/calling/pubsub .... is this a possibility? Is it the case that you want to process your messages in batch?Kolban
I have multiple projects that needs to consume messages from my project. Previously I have pushed messages to their topics in other projects. I would prefer a solution where I publish to a topic in my project. And let other projects pull messages. This will reduce the dependencies and I won't have to publish to and maintain 10 different topics of other projects.Calle Engene
My understanding is that the coupling in GCP Pub/Sub is the concept of publisher, topic and subscription. A topic doesn't hold messages ... it is the destination to which messages are published. Each application that wants to receive the messages creates its own subscriptions. What this seems to tell me is that each of your distinct applications could have its own subscription. This means that the subscribing applications control what messages they see.Kolban

1 Answers

1
votes

The error message is raised on this line:

const formattedSubscription = subClient.subscriptionPath(projectId, subscriptionName);

because your 'name_of_my_subscription' is projects/your-project/subscriptions/subname and not subname:

const subscriptionName = 'name_of_my_subscription'; 

That is why you get double name projects/projectname/subscriptions/ :

 could not instantiate a path template from 

projects/projectname/subscriptions/projects/projectname/subscriptions/test