@getcircuit/pubsub
Classes
This library exposes a set of utility classes to interact with Google's Pub/Sub.
PubSubPublisher
This class is responsible for setting up a publisher to publish messages to a
specific topic.
The topic must already exist.
StreamingSubscriber
This class is responsible for setting up a StreamingPull subscription to a
specific Pub/Sub subscription.
The Pub/Sub subscription must already exist.
This is the preferred class for setting up subscribers as it already handles
adding Tracing attributes to every received message and creates a span for each
automatically, as well as calls a callback for each message.
This class keeps an open connection that immediately receives and processes new
messages when available.
PollingSubscriber
This class is responsible for setting up a Pull subscription to a specific
Pub/Sub subscription.
The Pub/Sub subscription must already exist.
Contrary to the StreamingSubscriber class, this class does not add the Tracing
attributes of the messages to each span, because it pulls messages in batches
and on demand.
Temporary subscriptions
There are 4 functions that are able to create temporary subscriptions for
helping in local development:
getTemporaryStreamingSubscriberFromSubscription
- As the name implies, creates a new
StreamingSubscriber pointing to a
temporary subscription (1 day expiry time), from the topic that another
subscription points to. This is useful if you only have the subscription name
and not the topic name.
getTemporaryStreamingSubscriberFromTopic
- This is the same as the previous one, but it uses the topic name directly.
getTemporaryPollingSubscriberFromSubscription
- As the name implies, creates a new
PollingSubscriber pointing to a
temporary subscription (1 day expiry time), from the topic that another
subscription points to. This is useful if you only have the subscription name
and not the topic name.
getTemporaryPollingSubscriberFromTopic
- This is the same as the previous one, but it uses the topic name directly.
Testing utilities
There are 2 testing utilities provided by the library:
mockPubSubFromFirebaseApp
- It uses the provided Firebase Admin App to infer the current projectId and
creates and returns one instance of the
PubSubPublisher, one of the
StreamingSubscriber and one of the PollingSubscriber. They all share the
same - random - topic names and subscription names, which is useful to
ensure tests can run concurrently for Pub/Sub. This function thus should
only be ruen on an environemnt where the emulator is present
mockPubSub
- The same as the above, but uses the provided projectId directly