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
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
;
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.
void
subscription
.
on
(
'message'
,
message
=
>
{
message
.
ack
();
});
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.
subscription
.
on
(
'message'
,
async
(
message
)
=
>
{
const
response
=
await
message
.
ackWithResponse
();
});
modAck(deadline)
modAck
(
deadline
:
number
)
:
void
;
Modifies the ack deadline.
deadline
number
The number of seconds to extend the deadline.
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.
deadline
number
The number of seconds to extend the deadline.
nack()
nack
()
:
void
;
Removes the message from our inventory and schedules it to be redelivered.
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.
subscription
.
on
(
'message'
,
async
(
message
)
=
>
{
const
response
=
await
message
.
nackWithResponse
();
});