Message objects provide a simple interface for users to get message data and acknowledge the message.
Package
@google-cloud/pubsubExample
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 } |
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 | |
---|---|
Type | Description |
void |
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> |
subscription.on('message', async (message) => {
const response = await message.ackWithResponse();
});
modAck(deadline)
modAck(deadline: number): void;
Modifies the ack deadline.
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.
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 |
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> |
subscription.on('message', async (message) => {
const response = await message.nackWithResponse();
});