@sebspark/pubsub
TypeScript icon, indicating that this package has built-in type declarations

1.6.2 • Public • Published

@sebspark/pubsub

A wrapper around @google-cloud/pubsub adding simple methods for publishing and subscribing with typed messages.

Examples

type ExampleMessage = {
  messageType: string;
  message: string;
};

type ExamplePubsubChannels = {
  example: ExampleMessage;
};

SubscriptionClient

// Instantiate subscriber.
const subscriber = createSubscriber<ExamplePubsubChannels>({
  projectId: "test",
});

// Ensure
await subscriber.topic("example").initiate("example_subscription", {
  autoAck: false, // false means you need to call acc or nack in the message event handler.
  expirationPolicy: 3600 * 24, // Seconds.
  messageRetentionDuration: 3600 * 27 * 7, // Seconds
});

await subscriber.topic("example").subscribe("example_subscription", {
  onMessage: async (message: TypedMessage<ExampleMessage>) => {
    try {
      // Do something.

      // Ack message.
      message.ack();
    } catch (err) {
      console.error(err);

      // Nack message.
      message.nack();
    }
  },
});

PubSubOptions

SubscriptionClient supports a simplified subset of the options from @google-cloud/pubsub and a custom parameter that decides the ack/nack behavior of messages.

export type PubSubOptions = {
  expirationPolicy: number;
  messageRetentionDuration: number;
  autoAck?: boolean; // Default true
};

Set autoAck to false if you want to ack/nack messages manually in your message handler.

Package Sidebar

Install

npm i @sebspark/pubsub

Weekly Downloads

209

Version

1.6.2

License

Apache-2.0

Unpacked Size

27.2 kB

Total Files

7

Last publish

Collaborators

  • johanobrink
  • alexanderczigler
  • raduachim
  • believer