forked from googleapis/google-cloud-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscription.js
More file actions
329 lines (310 loc) · 9.4 KB
/
subscription.js
File metadata and controls
329 lines (310 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*!
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* @module pubsub/subscription
*/
'use strict';
var events = require('events');
var nodeutil = require('util');
/**
* @type {module:common/util}
* @private
*/
var util = require('../common/util.js');
/*! Developer Documentation
*
* @param {module:pubsub} pubsub - PubSub object.
* @param {object} options - Configuration object.
* @param {boolean} options.autoAck - Automatically acknowledge the message
* once it's pulled. (default: false)
* @param {number} options.interval - Interval in milliseconds to check for new
* messages. (default: 10)
* @param {string} options.name - Name of the subscription.
*/
/**
* A Subscription object will give you access to your Google Cloud Pub/Sub
* subscription.
*
* Subscriptions are sometimes retrieved when using various methods:
*
* - {@linkcode module:pubsub#getSubscriptions}
* - {@linkcode module:pubsub/topic#getSubscriptions}
* - {@linkcode module:pubsub/topic#subscribe}
*
* Subscription objects may be created directly with:
*
* - {@linkcode module:pubsub/topic#subscription}
*
* All Subscription objects are instances of an
* [EventEmitter](http://nodejs.org/api/events.html). The subscription will pull
* for messages automatically as long as there is at least one listener assigned
* for the `message` event.
*
* @alias pubsub/subscription
* @constructor
*
* @example
* //-
* // From {@linkcode module:pubsub#getSubscriptions}:
* //-
* pubsub.getSubscriptions(function(err, subscriptions) {
* // `subscriptions` is an array of Subscription objects.
* });
*
* //-
* // From {@linkcode module:pubsub/topic#getSubscriptions}:
* //-
* var topic = pubsub.topic('my-existing-topic');
* topic.getSubscriptions(function(err, subscriptions) {
* // `subscriptions` is an array of Subscription objects.
* });
*
* //-
* // From {@linkcode module:pubsub/topic#subscribe}:
* //-
* var topic = pubsub.topic('my-existing-topic');
* topic.subscribe('new-subscription', function(err, subscription) {
* // `subscription` is a Subscription object.
* });
*
* //-
* // From {@linkcode module:pubsub/topic#subscription}:
* //-
* var topic = pubsub.topic('my-existing-topic');
* var subscription = topic.subscription('my-existing-subscription');
* // `subscription` is a Subscription object.
*
* //-
* // Once you have obtained a subscription object, you may begin to register
* // listeners. This will automatically trigger pulling for messages.
* //-
* // Register an error handler.
* subscription.on('error', function(err) {});
*
* // Register a listener for `message` events.
* function onMessage(message) {
* // Called every time a message is received.
* // message.id = ID used to acknowledge its receival.
* // message.data = Contents of the message.
* }
* subscription.on('message', onMessage);
*
* // Remove the listener from receiving `message` events.
* subscription.removeListener('message', onMessage);
*/
function Subscription(pubsub, options) {
events.EventEmitter.call(this);
this.name = Subscription.formatName_(pubsub.projectId, options.name);
this.makeReq_ = pubsub.makeReq_.bind(pubsub);
this.autoAck = util.is(options.autoAck, 'boolean') ? options.autoAck : false;
this.closed = false;
this.interval = util.is(options.interval, 'number') ? options.interval : 10;
this.listenForEvents_();
}
nodeutil.inherits(Subscription, events.EventEmitter);
/**
* Format the name of a subscription. A subscription's full name is in the
* format of /subscription/{projectId}/{name}.
*
* @private
*/
Subscription.formatName_ = function(projectId, name) {
// Simple check if the name is already formatted.
if (name.indexOf('/') > -1) {
return name;
}
return '/subscriptions/' + projectId + '/' + name;
};
/**
* Simplify a message from an API response to have two properties, `id` and
* `data`. `data` is always converted to a string.
*
* @private
*/
Subscription.formatMessage_ = function(msg) {
var message = {
id: msg.ackId
};
var evt = msg.pubsubEvent;
if (evt && evt.message && evt.message.data) {
message.data = new Buffer(evt.message.data, 'base64').toString('utf-8');
try {
message.data = JSON.parse(message.data);
} catch(e) {}
}
return message;
};
/**
* Begin listening for events on the subscription. This method keeps track of
* how many message listeners are assigned, and then removed, making sure
* polling is handled automatically.
*
* As long as there is one active message listener, the connection is open. As
* soon as there are no more message listeners, the connection is closed.
*
* @private
*
* @example
* this.listenForEvents_();
*/
Subscription.prototype.listenForEvents_ = function() {
var that = this;
var messageListeners = 0;
this.on('newListener', function(event) {
if (event === 'message') {
messageListeners++;
if (that.closed) {
that.closed = false;
}
that.startPulling_();
}
});
this.on('removeListener', function(event) {
if (event === 'message' && --messageListeners === 0) {
that.closed = true;
}
});
};
/**
* Poll the backend for new messages. This runs a loop to ping the API at the
* provided interval from the subscription's instantiation. If one wasn't
* provided, the default value is 10 milliseconds.
*
* If messages are received, they are emitted on the `message` event.
*
* Note: This method is automatically called once a message event handler is
* assigned to the description.
*
* To stop pulling, see {@linkcode module:pubsub/subscription#close}.
*
* @private
*
* @example
* subscription.startPulling_();
*/
Subscription.prototype.startPulling_ = function() {
var that = this;
if (this.closed) {
return;
}
this.pull({
returnImmediately: false
}, function(err, message) {
if (err) {
that.emit('error', err);
}
if (message) {
that.emit('message', message);
}
setTimeout(that.startPulling_.bind(that), that.interval);
});
};
/**
* Acknowledge to the backend that the message was retrieved. You must provide
* either a single ID, or an array of IDs.
*
* @throws {Error} If at least one id is not provided.
*
* @param {string|string[]} ids - An ID or array of message IDs.
* @param {function} callback - The callback function.
*
* @example
* subscription.ack('ePHEESyhuE8e...', function(err) {});
*/
Subscription.prototype.ack = function(ids, callback) {
if (!ids || ids.length === 0) {
throw new Error(
'At least one ID must be specified before it can be acknowledged');
}
ids = util.arrayize(ids);
var body = {
subscription: this.name,
ackId: ids
};
this.makeReq_('POST', 'subscriptions/acknowledge', null, body, callback);
};
/**
* Delete the subscription. Pull requests from the current subscription will be
* errored once unsubscription is complete.
*
* @param {function=} callback - The callback function.
*
* @example
* subscription.delete(function(err) {});
*/
Subscription.prototype.delete = function(callback) {
callback = callback || util.noop;
this.makeReq_(
'DELETE', 'subscriptions/' + this.name, null, true, function(err) {
if (err) {
callback(err);
return;
}
this.closed = true;
this.removeAllListeners();
callback(null);
}.bind(this));
};
/**
* Pull messages from the subscribed topic. If messages were found, your
* callback is executed with the message object.
*
* Note that messages are pulled automatically once you register your first
* event listener to the subscription, thus the call to `pull` is handled for
* you. If you don't want to start pulling, simply don't register a
* `subscription.on('message', function() {})` event handler.
*
* @param {object=} options - Configuration object.
* @param {boolean=} options.returnImmediately - If set, the system will respond
* immediately. Otherwise, wait until new messages are available. Returns if
* timeout is reached.
* @param {function} callback - The callback function.
*
* @example
* subscription.pull(function(err, message) {
* // message.id = ID used to acknowledge its receival.
* // message.data = Contents of the message.
* });
*/
Subscription.prototype.pull = function(options, callback) {
var that = this;
// TODO(jbd): Should not be racing with other pull.
if (!callback) {
callback = options;
options = {};
}
var body = {
subscription: this.name,
returnImmediately: !!options.returnImmediately
};
this.makeReq_(
'POST', 'subscriptions/pull', null, body, function(err, message) {
// TODO(jbd): Fix API to return a list of messages.
if (err) {
callback(err);
return;
}
message = Subscription.formatMessage_(message);
if (that.autoAck) {
that.ack(message.id, function(err) {
callback(err, message);
});
} else {
callback(null, message);
}
});
};
module.exports = Subscription;