-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcometd.js
106 lines (85 loc) · 3.69 KB
/
cometd.js
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
// config file, you might want to exclude this file in .gitignore to keep confidential data
var config = require('./config.js');
// include required node modules
var url = require('url'),
http = require('http'),
https = require('https'),
request = require('request'),
faye = require('faye');
// fayeServer - a Bayeux server - is mounted at /cometd
var fayeServer = new faye.NodeAdapter({mount: '/cometd', timeout: 60 });
// Handle non-Bayeux requests
var server = http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Welcome to QuizVille Cometd server. It is mounted at /cometd.');
response.end();
});
fayeServer.attach(server);
server.listen(config.PORT);
// get Salesforce OAuth Token for access to APIs
function getOAuthToken(callback) {
var token_request = 'grant_type=password&client_id=' + config.CLIENT_ID +
'&client_secret=' + config.CLIENT_SECRET + '&username=' + config.USERNAME +
'&password=' + config.PASSWORD;
if(config.DEBUG) console.log('Sending token request '+ token_request);
request.post({
uri: config.LOGIN_SERVER + '/services/oauth2/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: token_request
}, function (error, response, body) {
if ( response.statusCode == 200 ) {
callback(JSON.parse(body));
} else {
if(config.DEBUG) console.log('Error '+response.statusCode+' '+body+' '+error);
}
});
}
// Get an OAuth token - and wait for callback
getOAuthToken(function(oauth) {
if(config.DEBUG) console.log('Got token '+ oauth.access_token);
// upstream cometd endpoint
var salesforce_endpoint = oauth.instance_url +'/cometd/24.0';
if(config.DEBUG) console.log("Creating a client for "+ salesforce_endpoint);
var upstreamClient = new faye.Client(salesforce_endpoint);
// set Authorization header
upstreamClient.setHeader('Authorization', 'OAuth '+ oauth.access_token);
// monitor connection down and reset the Authorization header
upstreamClient.bind('transport:down', function(upstreamClient) {
// get an OAuth token again
getOAuthToken(function(oauth) {
// set new Authorization header
upstreamClient.setHeader('Authorization', 'OAuth '+ oauth.access_token);
});
});
// just for debugging I/O, an extension to upstreamClient
upstreamClient.addExtension({
outgoing: function(message, callback) {
if(config.DEBUG) console.log('OUT >>> '+ JSON.stringify(message));
callback(message);
},
incoming: function(message, callback) {
if(config.DEBUG) console.log('IN >>>> '+ JSON.stringify(message));
callback(message);
}
});
// start downstreamClient to publish messages
var downstreamClient = fayeServer.getClient();
// subscribe to salesforce push topic
if(config.DEBUG) console.log('Subscribing to '+ config.PUSH_TOPIC);
var upstreamSub = upstreamClient.subscribe(config.PUSH_TOPIC, function(message) {
if(config.DEBUG) console.log("Received upstream message: " + JSON.stringify(message));
// publish back to downstream - organized by Quick_Quiz__c
if(config.DEBUG) console.log('Publishing to /q/'+ message.sobject.Quick_Quiz__c);
downstreamClient.publish('/q/'+ message.sobject.Quick_Quiz__c, message);
});
// log that upstream subscription is active
upstreamSub.callback(function() {
if(config.DEBUG) console.log('Upstream subscription is now active');
});
// log that upstream subscription encounters error
upstreamSub.errback(function(error) {
if(config.DEBUG) console.error("ERROR ON Upstream subscription Attempt: " + error.message);
});
});