Class Message (6.4.1)

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

Implements

tracing.MessageWithAttributes

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
Name Description
sub Subscriber

The parent subscriber.

{ ackId, message, deliveryAttempt } google.pubsub.v1.IReceivedMessage

Properties

ackId

ackId: string;

attributes

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

data

data: Buffer;

deliveryAttempt

deliveryAttempt: number;

handledPromise

get handledPromise(): Promise<void>;

Resolves when the message has been handled fully; a handled message may not have any further operations performed on it.

id

id: string;

length

get length(): number;

The length of the message data.

{number}

orderingKey

orderingKey?: string;

parentSpan

parentSpan?: tracing.Span;

Tracks a telemetry tracing parent span through the receive process. This will be the original publisher-side span if we have one; otherwise we'll create a "publisher" span to hang new subscriber spans onto.

This needs to be declared explicitly here, because having a public class implement a private interface seems to confuse TypeScript. (And it's needed in unit tests.)

publishTime

publishTime: PreciseDate;

received

received: number;

subSpans

subSpans: SubscriberSpans;

Tracks subscriber-specific telemetry objects through the library.

Methods

ack()

ack(): void;

Acknowledges the message.

Returns
Type Description
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
Name Description
error AckError
Returns
Type Description
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
Type Description
Promise<AckResponse>
Example

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

dispatched()

dispatched(): void;

When this message is dispensed to user callback code, this should be called. The time between the dispatch and the handledPromise resolving is when the message is with the user.

Returns
Type Description
void

endParentSpan()

endParentSpan(): void;

Ends any open subscribe telemetry tracing span.

Returns
Type Description
void

isDispatched()

isDispatched(): boolean;
Returns
Type Description
boolean

True if this message has been dispatched to user callback code.

modAck(deadline)

modAck(deadline: number): void;

Modifies the ack deadline. At present time, this should generally not be called by users.

Parameter
Name Description
deadline number

The number of seconds to extend the deadline.

Returns
Type Description
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. At present time, this should generally not be called by users.

Parameter
Name Description
deadline number

The number of seconds to extend the deadline.

Returns
Type Description
Promise<AckResponse>

nack()

nack(): void;

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

Returns
Type Description
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
Type Description
Promise<AckResponse>
Example

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