-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPullSubscriber.agent.nut
More file actions
115 lines (105 loc) · 4.68 KB
/
PullSubscriber.agent.nut
File metadata and controls
115 lines (105 loc) · 4.68 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
// MIT License
//
// Copyright 2017 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// OAuth 2.0 library required for GooglePubSub
#require "OAuth2.agent.lib.nut:2.0.0"
#require "GooglePubSub.agent.lib.nut:1.1.0"
// GooglePubSub.PullSubscriber and GooglePubSub.Subscriptions demo.
// Creates a pull subscription to the specified topic if it does not exist
// (topic name and subscription name are specified as constructor arguments),
// receives messages from it and prints messages data and attributes to the log.
// Messages are received using repeated pending pulls and acknowledged automatically
// using autoAck pull option.
class PullSubscriber {
_topicName = null;
_subscrName = null;
_topics = null;
_subscrs = null;
_pullSubscriber = null;
constructor(projectId, oAuthTokenProvider, topicName, subscrName) {
_topicName = topicName;
_subscrName = subscrName;
_topics = GooglePubSub.Topics(projectId, oAuthTokenProvider);
_subscrs = GooglePubSub.Subscriptions(projectId, oAuthTokenProvider);
_pullSubscriber = GooglePubSub.PullSubscriber(projectId, oAuthTokenProvider, subscrName);
}
// Handler function to be executed when messages are received
function onMessagesReceived(error, messages) {
if (error) {
server.error("Pull messages request failed: " + error.details);
}
else {
server.log("Messages received:");
foreach (msg in messages) {
server.log(format("data: %s, attrs: %s, publishTime: %s",
msg.data ? http.jsonencode(msg.data) : "null",
msg.attributes ? http.jsonencode(msg.attributes) : "null",
msg.publishTime));
}
}
}
// Checks if the specified subscription exists and optionally creates it if not,
// then receives messages from it using repeated pending pull
function subscribe() {
_topics.obtain(_topicName, null, function (error) {
if (error && error.type == PUB_SUB_ERROR.PUB_SUB_REQUEST_FAILED && error.httpStatus == 404) {
server.error(format("Topic %s doesn't exist. Impossible to create a subscription.", _topicName));
}
else {
local subscrOptions = {
"autoCreate" : true,
"subscrConfig" : GooglePubSub.SubscriptionConfig(_topicName)
};
_subscrs.obtain(_subscrName, subscrOptions, function (error, subscrConfig) {
if (error) {
server.error("Subscription obtain request failed: " + error.details);
}
else {
local pullOptions = {
"repeat" : true,
"autoAck" : true
};
_pullSubscriber.pendingPull(pullOptions, onMessagesReceived);
}
}.bindenv(this));
}
}.bindenv(this));
}
}
// Configuration constants, substitute with real values
const PROJECT_ID = "...";
const GOOGLE_ISS = "...";
const GOOGLE_SECRET_KEY = "...";
// obtaining OAuth2 Access Tokens Provider
local oAuthTokenProvider = OAuth2.JWTProfile.Client(
OAuth2.DeviceFlow.GOOGLE,
{
"iss" : GOOGLE_ISS,
"jwtSignKey" : GOOGLE_SECRET_KEY,
"scope" : "https://www.googleapis.com/auth/pubsub"
});
const TOPIC_NAME = "test_topic";
const SUBSCR_NAME = "test_pull_subscription";
// Start Application
pullSubscriber <- PullSubscriber(PROJECT_ID, oAuthTokenProvider, TOPIC_NAME, SUBSCR_NAME);
pullSubscriber.subscribe();