A toolkit of node.js scripts to manage Google cloud PubSub topics
- Get the relevant binary or source code from the node.js download page and install. Check node and node package manager version with
node -v
andnpm -v
respectively. - Clone this repository and cd into it:
git clone git@github.com:rowjay/pubsub-toolkit.git && cd pubsub-toolkit
- Install dependencies:
npm install
{
"data": string,
"attributes": {
string: string,
...
},
"messageId": string,
"publishTime": string,
}
- Create storage bucket for functions
- Write index.js, package.json, and config.json files
- Deploy function to be executed (in index.js) by upload, storage, or repo. Note this is configured with json files.
Functions can be triggered by HTTP, Pubsub message, or Storage events. Background functions take 'event' and 'callback' parameters
The event is a javascript object
{
'eventId': 'unique ID',
'timestamp': 'ISO 8601 created datetime',
'eventType': 'type of event',
'resource': 'what emitted this event'
'data': {
'key': 'value'
}
}
Signals completion
// Success
callback(null, 'Win!');
// Failure
callback(new Error('oops'));
Eg.
DATA=$(printf 'Example'|base64) && gcloud beta functions call functionName --data '{"x":"'$DATA'"}'
To manually invoke pubsub triggered function:
gcloud beta functions call functionName --data '{"data": "payload", "attributes": {"key0": "value0"}}'
Pubsub message format Functions events and callbacks PubSub API client node.js