Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/pubsub/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,11 @@ PubSub.prototype.subscribe = function(topic, subName, options, callback) {
* // Register a listener for `message` events.
* subscription.on('message', function(message) {
* // Called every time a message is received.
* // message.id = ID used to acknowledge its receival.
* // message.id = ID of the message.
* // message.ackId = ID used to acknowledge the message receival.
* // message.data = Contents of the message.
* // message.attributes = Attributes of the message.
* // message.timestamp = Timestamp when Pub/Sub received the message.
* });
*/
PubSub.prototype.subscription = function(name, options) {
Expand Down
17 changes: 15 additions & 2 deletions packages/pubsub/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ var PUBSUB_API_TIMEOUT = 90000;
* // message.id = ID of the message.
* // message.ackId = ID used to acknowledge the message receival.
* // message.data = Contents of the message.
* // message.attributes = Attributes of the message.
* // message.timestamp = Timestamp when Pub/Sub received the message.
*
* // Ack the message:
* // message.ack(callback);
Expand Down Expand Up @@ -332,8 +334,9 @@ function Subscription(pubsub, options) {
modelo.inherits(Subscription, common.GrpcServiceObject, events.EventEmitter);

/**
* Simplify a message from an API response to have three properties, `id`,
* `data` and `attributes`. `data` is always converted to a string.
* Simplify a message from an API response to have five properties: `id`,
* `ackId`, `data`, `attributes`, and `timestamp`. `data` is always converted to
* a string.
*
* @private
*/
Expand All @@ -357,6 +360,16 @@ Subscription.formatMessage_ = function(msg, encoding) {
if (innerMessage.attributes) {
message.attributes = innerMessage.attributes;
}

if (innerMessage.publishTime) {
var publishTime = innerMessage.publishTime;

if (publishTime.seconds && publishTime.nanos) {
var seconds = parseInt(publishTime.seconds, 10);
var milliseconds = parseInt(publishTime.nanos, 10) / 1e6;
message.timestamp = new Date(seconds * 1000 + milliseconds);
}
}
}

return message;
Expand Down
4 changes: 3 additions & 1 deletion packages/pubsub/src/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,11 @@ Topic.prototype.subscribe = function(subName, options, callback) {
* // Register a listener for `message` events.
* subscription.on('message', function(message) {
* // Called every time a message is received.
* // message.id = ID used to acknowledge its receival.
* // message.id = ID of the message.
* // message.ackId = ID used to acknowledge the message receival.
* // message.data = Contents of the message.
* // message.attributes = Attributes of the message.
* // message.timestamp = Timestamp when Pub/Sub received the message.
* });
*/
Topic.prototype.subscription = function(name, options) {
Expand Down
14 changes: 12 additions & 2 deletions packages/pubsub/test/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,31 @@ describe('Subscription', function() {
var obj = { hi: 'there' };
var stringified = new Buffer(JSON.stringify(obj)).toString('base64');
var attributes = {};
var publishTime = {
seconds: '1480413405',
nanos: 617000000
};

var seconds = parseInt(publishTime.seconds, 10);
var milliseconds = parseInt(publishTime.nanos, 10) / 1e6;
var expectedDate = new Date(seconds * 1000 + milliseconds);

var msg = Subscription.formatMessage_({
ackId: 'abc',
message: {
data: stringified,
messageId: 7,
attributes: attributes
attributes: attributes,
publishTime: publishTime
}
});

assert.deepEqual(msg, {
ackId: 'abc',
id: 7,
data: obj,
attributes: attributes
attributes: attributes,
timestamp: expectedDate
});
});

Expand Down