@web-artisans/gcp-subscriber

A subscription helper for Google Cloud Pub/Sub.

Usage no npm install needed!

<script type="module">
  import webArtisansGcpSubscriber from 'https://cdn.skypack.dev/@web-artisans/gcp-subscriber';
</script>

README

Google Cloud Platform Subscriber

This project is a simple, lightweight helper library for Google Cloud Pub/Sub subscription management.

How do I get set up?

  1. npm install --save @web-artisans/gcp-subscriber
  2. const subscriber = require('@web-artisans/gcp-subscriber')(pubsub, logger);

Usage

Message Acknowledgement (acks)

If the supplied onMessage handler returns a Promise, the subscriber can ack the message automatically when the handler completes successfully. If not, you must acknowledge the message yourself in the onMessage handler function.

By default, errors are not acknowledged.

subscriber.map

The map function takes an object whose keys are valid topic names and whose attributes are objects containing onMessage and onError handlers. For example:

const subscriber = require('@web-artisans/gcp-subscriber')(pubsub, logger);

let subscriptions = {
    console-log: {
        onMessage: function(message) {
            console.log(message);
            message.ack(); //Manually ack since we don't return a promise
        },
        onError: function(error) {
            throw error;
        }
    },
    database-log: {
        onMessage: function(message) {
            return db.table('log').insert({message}); //No ack required here since insert returns a Promise.
        },
        onError: function(error) {
            throw error;
        }
    }
};

subscriber.map(subscriptions);

Who do I talk to?