Class Message (4.0.4)

Message objects provide a simple interface for users to get message data and acknowledge the message.

Package

@google-cloud/pubsub

Example


subscription.on('message', message => {
  // {
  //   ackId: 'RUFeQBJMJAxESVMrQwsqWBFOBCEhPjA',
  //   attributes: {key: 'value'},
  //   data: Buffer.from('Hello, world!'),
  //   id: '1551297743043',
  //   orderingKey: 'ordering-key',
  //   publishTime: new PreciseDate('2019-02-27T20:02:19.029534186Z'),
  //   received: 1551297743043,
  //   length: 13
  // }
});

Constructors

(constructor)(sub, { ackId, message, deliveryAttempt })

constructor(sub: Subscriber, { ackId, message, deliveryAttempt }: google.pubsub.v1.IReceivedMessage);

Constructs a new instance of the Message class

Parameters
NameDescription
sub Subscriber

The parent subscriber.

{ ackId, message, deliveryAttempt } IReceivedMessage

Properties

ackId

ackId: string;

attributes

attributes: {
        [key: string]: string;
    };

data

data: Buffer;

deliveryAttempt

deliveryAttempt: number;

id

id: string;

length

get length(): number;

The length of the message data.

{number}

orderingKey

orderingKey?: string;

publishTime

publishTime: PreciseDate;

received

received: number;

Methods

ack()

ack(): void;

Acknowledges the message.

Returns
TypeDescription
void
Example

subscription.on('message', message => {
  message.ack();
});

ackFailed(error)

ackFailed(error: AckError): void;

Sets this message's exactly once delivery acks to permanent failure. This is meant for internal library use only.

Parameter
NameDescription
error AckError
Returns
TypeDescription
void

ackWithResponse()

ackWithResponse(): Promise<AckResponse>;

Acknowledges the message, expecting a response (for exactly-once delivery subscriptions). If exactly-once delivery is not enabled, this will immediately resolve successfully.

Returns
TypeDescription
Promise<AckResponse>
Example

subscription.on('message', async (message) => {
  const response = await message.ackWithResponse();
});

modAck(deadline)

modAck(deadline: number): void;

Modifies the ack deadline.

Parameter
NameDescription
deadline number

The number of seconds to extend the deadline.

Returns
TypeDescription
void

modAckWithResponse(deadline)

modAckWithResponse(deadline: number): Promise<AckResponse>;

Modifies the ack deadline, expecting a response (for exactly-once delivery subscriptions). If exactly-once delivery is not enabled, this will immediately resolve successfully.

Parameter
NameDescription
deadline number

The number of seconds to extend the deadline.

Returns
TypeDescription
Promise<AckResponse>

nack()

nack(): void;

Removes the message from our inventory and schedules it to be redelivered.

Returns
TypeDescription
void
Example

subscription.on('message', message => {
  message.nack();
});

nackWithResponse()

nackWithResponse(): Promise<AckResponse>;

Removes the message from our inventory and schedules it to be redelivered, with the modAck response being returned (for exactly-once delivery subscriptions). If exactly-once delivery is not enabled, this will immediately resolve successfully.

Returns
TypeDescription
Promise<AckResponse>
Example

subscription.on('message', async (message) => {
  const response = await message.nackWithResponse();
});