Skip to content

Commit

Permalink
pubsub: return timestamp on message (#1843)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored and callmehiphop committed Nov 29, 2016
1 parent 50082da commit 85b42b4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
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

0 comments on commit 85b42b4

Please sign in to comment.