Skip to content

Commit 5374649

Browse files
pubsub: allow simple publishing
1 parent 793e911 commit 5374649

File tree

5 files changed

+92
-47
lines changed

5 files changed

+92
-47
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,7 @@ var pubsubClient = pubsub({
425425
var topic = pubsubClient.topic('my-topic');
426426

427427
// Publish a message to the topic.
428-
topic.publish({
429-
data: 'New message!'
430-
}, function(err) {});
428+
topic.publish('New message!', function(err) {});
431429

432430
// Subscribe to the topic.
433431
var options = {

packages/pubsub/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ var pubsub = require('@google-cloud/pubsub')({
2020
var topic = pubsub.topic('my-topic');
2121

2222
// Publish a message to the topic.
23-
topic.publish({
24-
data: 'New message!'
25-
}, function(err) {});
23+
topic.publish('New message!', function(err) {});
2624

2725
// Subscribe to the topic.
2826
var options = {

packages/pubsub/src/topic.js

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
var arrify = require('arrify');
2424
var common = require('@google-cloud/common');
2525
var is = require('is');
26-
var prop = require('propprop');
2726
var util = require('util');
2827

2928
/**
@@ -282,32 +281,27 @@ Topic.prototype.getSubscriptions = function(options, callback) {
282281
* @resource [Topics: publish API Documentation]{@link https://cloud.google.com/pubsub/reference/rest/v1/projects.topics/publish}
283282
*
284283
* @throws {Error} If no message is provided.
285-
* @throws {Error} If a message is missing a data property.
286284
*
287-
* @param {object|object[]} message - The message(s) to publish.
288-
* @param {*} message.data - The contents of the message.
289-
* @param {array=} message.attributes - Key/value pair of attributes to apply to
290-
* the message. All values must be strings.
285+
* @param {*|*[]} message - The message(s) to publish. If you need to
286+
* provide attributes for the message, you must enable `options.raw`, then
287+
* box your message in to an object with a `data` and `attributes` property.
288+
* `data` will be the raw message value you want to publish, and
289+
* `attributes` is a key/value pair of attributes to apply to the message.
290+
* @param {object=} options - Configuration object.
291+
* @param {boolean} options.raw - Enable if you require setting attributes on
292+
* your messages.
291293
* @param {function=} callback - The callback function.
292294
*
293295
* @example
294-
* topic.publish({
295-
* data: 'Hello, world!'
296-
* }, function(err, messageIds, apiResponse) {});
296+
* topic.publish('Hello, world!', function(err, messageIds, apiResponse) {});
297297
*
298298
* //-
299-
* // The data property can be a JSON object as well.
299+
* // You can also publish a JSON object.
300300
* //-
301301
* var registerMessage = {
302-
* data: {
303-
* userId: 3,
304-
* name: 'Stephen',
305-
* event: 'new user'
306-
* },
307-
* attributes: {
308-
* key: 'value',
309-
* hello: 'world'
310-
* }
302+
* userId: 3,
303+
* name: 'Stephen',
304+
* event: 'new user'
311305
* };
312306
*
313307
* topic.publish(registerMessage, function(err, messageIds, apiResponse) {});
@@ -327,28 +321,55 @@ Topic.prototype.getSubscriptions = function(options, callback) {
327321
* registerMessage,
328322
* purchaseMessage
329323
* ], function(err, messageIds, apiResponse) {});
324+
*
325+
* //-
326+
* // Set attributes with your message.
327+
* //-
328+
* var message = {
329+
* data: {
330+
* userId: 3,
331+
* product: 'book',
332+
* event: 'rent'
333+
* },
334+
* attributes: {
335+
* key: 'value',
336+
* hello: 'world'
337+
* }
338+
* };
339+
*
340+
* var options = {
341+
* raw: true
342+
* };
343+
*
344+
* topic.publish(message, options, function(err, messageIds, apiResponse) {});
330345
*/
331-
Topic.prototype.publish = function(messages, callback) {
346+
Topic.prototype.publish = function(messages, options, callback) {
332347
messages = arrify(messages);
333348

334-
if (messages.length === 0) {
335-
throw new Error('Cannot publish without a message.');
336-
}
337-
338-
if (!messages.every(prop('data'))) {
339-
throw new Error('Cannot publish message without a `data` property.');
349+
if (is.fn(options)) {
350+
callback = options;
351+
options = {};
340352
}
341353

354+
options = options || {};
342355
callback = callback || common.util.noop;
343356

357+
if (messages.length === 0) {
358+
throw new Error('Cannot publish without a message.');
359+
}
360+
344361
var protoOpts = {
345362
service: 'Publisher',
346363
method: 'publish',
347364
};
348365

349366
var reqOpts = {
350367
topic: this.name,
351-
messages: messages.map(Topic.formatMessage_)
368+
messages: messages
369+
.map(function(message) {
370+
return options.raw ? message : { data: message };
371+
})
372+
.map(Topic.formatMessage_)
352373
};
353374

354375
this.request(protoOpts, reqOpts, function(err, result) {

packages/pubsub/system-test/pubsub.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,23 @@ describe('pubsub', function() {
120120

121121
it('should publish a message', function(done) {
122122
var topic = pubsub.topic(TOPIC_NAMES[0]);
123-
topic.publish({ data: 'message from me' }, function(err, messageIds) {
123+
topic.publish('message from me', function(err, messageIds) {
124124
assert.ifError(err);
125-
assert.equal(messageIds.length, 1);
125+
assert.strictEqual(messageIds.length, 1);
126+
done();
127+
});
128+
});
129+
130+
it('should publish a message with attributes', function(done) {
131+
var topic = pubsub.topic(TOPIC_NAMES[1]);
132+
topic.publish({
133+
data: 'raw message data',
134+
attributes: {
135+
raw: true
136+
}
137+
}, function(err, messageIds) {
138+
assert.ifError(err);
139+
assert.strictEqual(messageIds.length, 1);
126140
done();
127141
});
128142
});
@@ -166,7 +180,7 @@ describe('pubsub', function() {
166180
}
167181

168182
async.times(10, function(_, next) {
169-
topic.publish({ data: 'hello' }, next);
183+
topic.publish('hello', next);
170184
}, function(err) {
171185
if (err) {
172186
done(err);

packages/pubsub/test/topic.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ describe('Topic', function() {
180180

181181
describe('publish', function() {
182182
var message = 'howdy';
183-
var messageObject = { data: message };
183+
var attributes = {
184+
key: 'value'
185+
};
184186

185187
it('should throw if no message is provided', function() {
186188
assert.throws(function() {
@@ -192,12 +194,6 @@ describe('Topic', function() {
192194
}, /Cannot publish without a message\./);
193195
});
194196

195-
it('should throw if a message has no data', function() {
196-
assert.throws(function() {
197-
topic.publish({});
198-
}, /Cannot publish message without a `data` property\./);
199-
});
200-
201197
it('should send correct api request', function(done) {
202198
topic.request = function(protoOpts, reqOpts) {
203199
assert.strictEqual(protoOpts.service, 'Publisher');
@@ -211,15 +207,33 @@ describe('Topic', function() {
211207
done();
212208
};
213209

214-
topic.publish(messageObject, assert.ifError);
210+
topic.publish(message, assert.ifError);
211+
});
212+
213+
it('should send correct api request for raw message', function(done) {
214+
topic.request = function(protoOpts, reqOpts) {
215+
assert.deepEqual(reqOpts.messages, [
216+
{
217+
data: new Buffer(JSON.stringify(message)).toString('base64'),
218+
attributes: attributes
219+
}
220+
]);
221+
222+
done();
223+
};
224+
225+
topic.publish({
226+
data: message,
227+
attributes: attributes
228+
}, { raw: true }, assert.ifError);
215229
});
216230

217231
it('should execute callback', function(done) {
218232
topic.request = function(protoOpts, reqOpts, callback) {
219233
callback(null, {});
220234
};
221235

222-
topic.publish(messageObject, done);
236+
topic.publish(message, done);
223237
});
224238

225239
it('should execute callback with error', function(done) {
@@ -230,7 +244,7 @@ describe('Topic', function() {
230244
callback(error, apiResponse);
231245
};
232246

233-
topic.publish(messageObject, function(err, ackIds, apiResponse_) {
247+
topic.publish(message, function(err, ackIds, apiResponse_) {
234248
assert.strictEqual(err, error);
235249
assert.strictEqual(ackIds, null);
236250
assert.strictEqual(apiResponse_, apiResponse);
@@ -246,7 +260,7 @@ describe('Topic', function() {
246260
callback(null, resp);
247261
};
248262

249-
topic.publish(messageObject, function(err, ackIds, apiResponse) {
263+
topic.publish(message, function(err, ackIds, apiResponse) {
250264
assert.deepEqual(resp, apiResponse);
251265
done();
252266
});

0 commit comments

Comments
 (0)