Skip to content

Commit 436cb4a

Browse files
Step 7 - Connect the worker to Wolfram Alpha
1 parent 8f20805 commit 436cb4a

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

example.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const Express = require('express');
33
const ServerPort = 3000;
44
const worker = require('./worker');
55

6-
const ApiKey = 'INSERT-YOUR-API-KEY-HERE'; /* Add your API key here */
7-
if (ApiKey.indexOf('INSERT') === 0) { throw('Cannot run without an API key. Add your key to example.js'); }
6+
const ApiKey = 'INSERT-YOUR-API-KEY-HERE'; /* Add your Ably API key here */
7+
if (ApiKey.indexOf('INSERT') === 0) { throw('Cannot run without an Ably API key. Add your key to example.js'); }
8+
9+
const WolframAppId = 'INSERT-YOUR-WOLFRAM-API-KEY-HERE'; /* Add your Wolfram AppID here */
10+
if (ApiKey.indexOf('INSERT') === 0) { throw('Cannot run without a Wolfram API key. Add your key to example.js'); }
811

912
/* Instance the Ably library */
1013
var rest = new Ably.Rest({ key: ApiKey });
@@ -29,6 +32,6 @@ app.get('/auth', function (req, res) {
2932
app.use(express.static('public'));
3033
app.listen(3000);
3134

32-
worker.start(ApiKey, 'wolfram:answers', 'wolfram', 'us-east-1-a-queue.ably.io:5671/shared');
35+
worker.start(ApiKey, WolframAppId, 'wolfram:answers', 'wolfram', 'us-east-1-a-queue.ably.io:5671/shared');
3336

3437
console.log('Open the Wolfram demo in your browser: https://localhost:' + ServerPort + '/');

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"ably": ">=0.9.0-beta",
1515
"amqplib": "^0.4",
16-
"express": ">=4.14.0"
16+
"express": ">=4.14.0",
17+
"request": ">=2.79.0"
1718
}
1819
}

worker.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,38 @@
22

33
const amqp = require('amqplib/callback_api');
44
const Ably = require('ably');
5+
const request = require('request');
6+
const querystring = require("querystring");
7+
const WolframEndpoint = 'http://api.wolframalpha.com/v1/result';
8+
9+
/* Send question over HTTP to Wolfram to find the answer */
10+
function getAnswerAndPublish(ablyChannel, wolframUrl, question) {
11+
console.log('worker:', 'Received question', question, ' - about to ask Wolfram');
12+
var timeNow = Date.now();
13+
request(wolframUrl, function (error, response, body) {
14+
var timePassed = Date.now() - timeNow;
15+
if (!error && response.statusCode == 200) {
16+
publishAnswer(ablyChannel, question, body, timePassed)
17+
} else {
18+
if (body) {
19+
publishAnswer(ablyChannel, question, "Wolfram couldn't compute: " + body, timePassed);
20+
} else {
21+
publishAnswer(ablyChannel, question, "Wolfram error: " + JSON.stringify(error), timePassed);
22+
}
23+
}
24+
});
25+
}
26+
27+
function publishAnswer(ablyChannel, question, answer, wolframTime) {
28+
ablyChannel.publish('answer', { question: question, answer: answer, wolframTime: wolframTime }, function(err) {
29+
if (err) {
30+
console.error('worker:', 'Failed to publish question', question, ' - err:', JSON.stringify(err));
31+
}
32+
})
33+
}
534

635
/* Start the worker that consumes from the AMQP QUEUE */
7-
exports.start = function(apiKey, answersChannelName, queueName, queueEndpoint) {
36+
exports.start = function(apiKey, wolframApiKey, answersChannelName, queueName, queueEndpoint) {
837
const appId = apiKey.split('.')[0];
938
const queue = appId + ":" + queueName;
1039
const endpoint = queueEndpoint;
@@ -34,12 +63,9 @@ exports.start = function(apiKey, answersChannelName, queueName, queueEndpoint) {
3463

3564
const messages = Ably.Realtime.Message.fromEncodedArray(decodedEnvelope.messages);
3665
messages.forEach(function(message) {
37-
console.log('worker:', 'Received question', message.data, ' - about to ask Wolfram');
38-
answersChannel.publish('answer', 'Placeholder until Wolfram connected', function(err) {
39-
if (err) {
40-
console.error('worker:', 'Failed to publish question', message.data, ' - err:', JSON.stringify(err));
41-
}
42-
})
66+
var question = message.data;
67+
var url = WolframEndpoint + '?' + querystring.stringify({ appid: wolframApiKey, i: question});
68+
getAnswerAndPublish(answersChannel, url, question);
4369
});
4470

4571
/* Remove message from queue */

0 commit comments

Comments
 (0)