23
23
24
24
'use strict' ;
25
25
26
- const pubsubClient = require ( `@google-cloud/pubsub` ) ( ) ;
26
+ const PubSub = require ( `@google-cloud/pubsub` ) ;
27
27
28
28
// [START pubsub_list_subscriptions]
29
29
function listSubscriptions ( callback ) {
30
+ // Instantiates the client library
31
+ const pubsubClient = PubSub ( ) ;
32
+
30
33
// Lists all subscriptions in the current project
31
34
pubsubClient . getSubscriptions ( ( err , subscriptions ) => {
32
35
if ( err ) {
@@ -43,6 +46,9 @@ function listSubscriptions (callback) {
43
46
44
47
// [START pubsub_list_topic_subscriptions]
45
48
function listTopicSubscriptions ( topicName , callback ) {
49
+ // Instantiates the client library
50
+ const pubsubClient = PubSub ( ) ;
51
+
46
52
// References an existing topic, e.g. "my-topic"
47
53
const topic = pubsubClient . topic ( topicName ) ;
48
54
@@ -62,6 +68,9 @@ function listTopicSubscriptions (topicName, callback) {
62
68
63
69
// [START pubsub_create_subscription]
64
70
function createSubscription ( topicName , subscriptionName , callback ) {
71
+ // Instantiates the client library
72
+ const pubsubClient = PubSub ( ) ;
73
+
65
74
// References an existing topic, e.g. "my-topic"
66
75
const topic = pubsubClient . topic ( topicName ) ;
67
76
@@ -80,16 +89,18 @@ function createSubscription (topicName, subscriptionName, callback) {
80
89
81
90
// [START pubsub_create_push_subscription]
82
91
function createPushSubscription ( topicName , subscriptionName , callback ) {
92
+ // Instantiates the client library
93
+ const pubsubClient = PubSub ( ) ;
94
+
83
95
// References an existing topic, e.g. "my-topic"
84
96
const topic = pubsubClient . topic ( topicName ) ;
85
- const projectId = process . env . GCLOUD_PROJECT || 'YOU_PROJECT_ID' ;
86
97
87
98
// Creates a new push subscription, e.g. "my-new-subscription"
88
99
topic . subscribe ( subscriptionName , {
89
100
pushConfig : {
90
101
// Set to an HTTPS endpoint of your choice. If necessary, register
91
102
// (authorize) the domain on which the server is hosted.
92
- pushEndpoint : `https://${ projectId } .appspot.com/push`
103
+ pushEndpoint : `https://${ pubsubClient . projectId } .appspot.com/push`
93
104
}
94
105
} , ( err , subscription ) => {
95
106
if ( err ) {
@@ -105,6 +116,9 @@ function createPushSubscription (topicName, subscriptionName, callback) {
105
116
106
117
// [START pubsub_delete_subscription]
107
118
function deleteSubscription ( subscriptionName , callback ) {
119
+ // Instantiates the client library
120
+ const pubsubClient = PubSub ( ) ;
121
+
108
122
// References an existing subscription, e.g. "my-subscription"
109
123
const subscription = pubsubClient . subscription ( subscriptionName ) ;
110
124
@@ -121,8 +135,11 @@ function deleteSubscription (subscriptionName, callback) {
121
135
}
122
136
// [END pubsub_delete_subscription]
123
137
124
- // [START pubsub_get_subscription_metadata]
125
- function getSubscriptionMetadata ( subscriptionName , callback ) {
138
+ // [START pubsub_get_subscription]
139
+ function getSubscription ( subscriptionName , callback ) {
140
+ // Instantiates the client library
141
+ const pubsubClient = PubSub ( ) ;
142
+
126
143
// References an existing subscription, e.g. "my-subscription"
127
144
const subscription = pubsubClient . subscription ( subscriptionName ) ;
128
145
@@ -140,10 +157,13 @@ function getSubscriptionMetadata (subscriptionName, callback) {
140
157
callback ( ) ;
141
158
} ) ;
142
159
}
143
- // [END pubsub_get_subscription_metadata ]
160
+ // [END pubsub_get_subscription ]
144
161
145
162
// [START pubsub_pull_messages]
146
163
function pullMessages ( subscriptionName , callback ) {
164
+ // Instantiates the client library
165
+ const pubsubClient = PubSub ( ) ;
166
+
147
167
// References an existing subscription, e.g. "my-subscription"
148
168
const subscription = pubsubClient . subscription ( subscriptionName ) ;
149
169
@@ -168,8 +188,73 @@ function pullMessages (subscriptionName, callback) {
168
188
}
169
189
// [END pubsub_pull_messages]
170
190
191
+ let subscribeCounterValue = 1 ;
192
+
193
+ function getSubscribeCounterValue ( ) {
194
+ return subscribeCounterValue ;
195
+ }
196
+
197
+ function setSubscribeCounterValue ( value ) {
198
+ subscribeCounterValue = value ;
199
+ }
200
+
201
+ // [START pubsub_pull_ordered_messages]
202
+ const outstandingMessages = { } ;
203
+
204
+ function pullOrderedMessages ( subscriptionName , callback ) {
205
+ // Instantiates the client library
206
+ const pubsubClient = PubSub ( ) ;
207
+
208
+ // References an existing subscription, e.g. "my-subscription"
209
+ const subscription = pubsubClient . subscription ( subscriptionName ) ;
210
+
211
+ // Pulls messages. Set returnImmediately to false to block until messages are
212
+ // received.
213
+ subscription . pull ( { returnImmediately : true } , ( err , messages ) => {
214
+ if ( err ) {
215
+ callback ( err ) ;
216
+ return ;
217
+ }
218
+
219
+ // Pub/Sub messages are unordered, so here we manually order messages by
220
+ // their "counterId" attribute which was set when they were published.
221
+ messages . forEach ( ( message ) => {
222
+ outstandingMessages [ message . attributes . counterId ] = message ;
223
+ } ) ;
224
+
225
+ const outstandingIds = Object . keys ( outstandingMessages ) . map ( ( counterId ) => + counterId ) ;
226
+ outstandingIds . sort ( ) ;
227
+
228
+ outstandingIds . forEach ( ( counterId ) => {
229
+ const counter = getSubscribeCounterValue ( ) ;
230
+ const message = outstandingMessages [ counterId ] ;
231
+
232
+ if ( counterId < counter ) {
233
+ // The message has already been processed
234
+ subscription . ack ( message . ackId ) ;
235
+ delete outstandingMessages [ counterId ] ;
236
+ } else if ( counterId === counter ) {
237
+ // Process the message
238
+ console . log ( `* %d %j %j` , message . id , message . data , message . attributes ) ;
239
+
240
+ setSubscribeCounterValue ( counterId + 1 ) ;
241
+ subscription . ack ( message . ackId ) ;
242
+ delete outstandingMessages [ counterId ] ;
243
+ } else {
244
+ // Have not yet processed the message on which this message is dependent
245
+ return false ;
246
+ }
247
+ } ) ;
248
+ callback ( ) ;
249
+ } ) ;
250
+ }
251
+ // [END pubsub_pull_ordered_messages]
252
+
171
253
// [START pubsub_get_subscription_policy]
172
254
function getSubscriptionPolicy ( subscriptionName , callback ) {
255
+ // Instantiates the client library
256
+ const pubsubClient = PubSub ( ) ;
257
+
173
258
// References an existing subscription, e.g. "my-subscription"
174
259
const subscription = pubsubClient . subscription ( subscriptionName ) ;
175
260
@@ -188,6 +273,9 @@ function getSubscriptionPolicy (subscriptionName, callback) {
188
273
189
274
// [START pubsub_set_subscription_policy]
190
275
function setSubscriptionPolicy ( subscriptionName , callback ) {
276
+ // Instantiates the client library
277
+ const pubsubClient = PubSub ( ) ;
278
+
191
279
// References an existing subscription, e.g. "my-subscription"
192
280
const subscription = pubsubClient . subscription ( subscriptionName ) ;
193
281
@@ -222,6 +310,9 @@ function setSubscriptionPolicy (subscriptionName, callback) {
222
310
223
311
// [START pubsub_test_subscription_permissions]
224
312
function testSubscriptionPermissions ( subscriptionName , callback ) {
313
+ // Instantiates the client library
314
+ const pubsubClient = PubSub ( ) ;
315
+
225
316
// References an existing subscription, e.g. "my-subscription"
226
317
const subscription = pubsubClient . subscription ( subscriptionName ) ;
227
318
@@ -253,8 +344,9 @@ const program = module.exports = {
253
344
createSubscription : createSubscription ,
254
345
createPushSubscription : createPushSubscription ,
255
346
deleteSubscription : deleteSubscription ,
256
- getSubscriptionMetadata : getSubscriptionMetadata ,
347
+ getSubscription : getSubscription ,
257
348
pullMessages : pullMessages ,
349
+ pullOrderedMessages : pullOrderedMessages ,
258
350
getSubscriptionPolicy : getSubscriptionPolicy ,
259
351
setSubscriptionPolicy : setSubscriptionPolicy ,
260
352
testSubscriptionPermissions : testSubscriptionPermissions ,
283
375
program . deleteSubscription ( options . subscriptionName , makeHandler ( false ) ) ;
284
376
} )
285
377
. command ( `get <subscriptionName>` , `Gets the metadata for a subscription.` , { } , ( options ) => {
286
- program . getSubscriptionMetadata ( options . subscriptionName , makeHandler ( false ) ) ;
378
+ program . getSubscription ( options . subscriptionName , makeHandler ( false ) ) ;
287
379
} )
288
380
. command ( `pull <subscriptionName>` , `Pulls messages for a subscription.` , { } , ( options ) => {
289
381
program . pullMessages ( options . subscriptionName , makeHandler ( false ) ) ;
0 commit comments