Skip to content

Commit 0ab1cb6

Browse files
Step 4 - Plugin in Ably client-side and update UI
1 parent 8c8556e commit 0ab1cb6

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

example.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ var rest = new Ably.Rest({ key: ApiKey });
1212
var express = require('express');
1313
var app = express();
1414

15+
/* Issue token requests to browser clients sending a request to the /auth endpoint */
16+
app.get('/auth', function (req, res) {
17+
rest.auth.createTokenRequest(function(err, tokenRequest) {
18+
if (err) {
19+
res.status(500).send('Error requesting token: ' + JSON.stringify(err));
20+
} else {
21+
res.setHeader('Content-Type', 'application/json');
22+
res.send(JSON.stringify(tokenRequest));
23+
}
24+
});
25+
});
26+
1527
/* Server static HTML files from /public folder */
1628
app.use(express.static('public'));
1729
app.listen(3000);

public/app.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,49 @@
11
$(function() {
2-
var $askButton = $('#ask-btn');
2+
/* Set up realtime library and authenticate using token issued from server */
3+
var ably = new Ably.Realtime({ authUrl: '/auth' });
4+
var answersChannel = ably.channels.get('wolfram:answers');
5+
var questionsChannel = ably.channels.get('wolfram:questions');
6+
7+
var $answers = $('#answers'),
8+
$status = $('#status'),
9+
$askButton = $('#ask-btn'),
10+
$question = $('#question');
11+
12+
/* Subscribe to answers published on this channel */
13+
answersChannel.subscribe(function(message) {
14+
var $question = $('<div class="question">').text(message.data.question);
15+
var $answer = $('<div class="answer">').text(message.data.answer);
16+
var $stat = $('<div class="stat">').text("Wolfram took " + message.data.wolframTime + "ms");
17+
$answers.prepend($('<div>').append($stat).append($question).append($answer));
18+
});
19+
320
$askButton.on('click', function() {
4-
alert("Not yet implemented");
21+
var question = $question.val();
22+
if (question.replace(' ') != '') {
23+
/* Publish question to the Ably channel so that the queue worker receives it via queues */
24+
questionsChannel.publish('question', question, function(err) {
25+
if (err) {
26+
showStatus('Failed to publish question!');
27+
$question.val(question);
28+
return;
29+
}
30+
clearStatus();
31+
});
32+
showStatus('Sending question...');
33+
$question.val('');
34+
}
535
});
36+
37+
ably.connection.on('connecting', function() { showStatus('Connecting to Ably...'); });
38+
ably.connection.on('connected', function() { clearStatus(); });
39+
ably.connection.on('disconnected', function() { showStatus('Disconnected from Ably...'); });
40+
ably.connection.on('suspended', function() { showStatus('Disconnected from Ably for a while...'); });
41+
42+
function showStatus(text) {
43+
$status.text(text).show();
44+
}
45+
46+
function clearStatus() {
47+
$status.fadeOut(750);
48+
}
649
});

public/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<title>Ably Reactor Queue and Wolfram Alpha demo</title>
44
<link rel="stylesheet" type="text/css" href="style.css">
55
<script src="//code.jquery.com/jquery-3.1.1.min.js" crossorigin="anonymous"></script>
6+
<script src="//cdn.ably.io/lib/ably.min-0.9.js" crossorigin="anonymous"></script>
67
<script type="text/javascript" src="app.js"></script>
78
</head>
89
<body>
@@ -17,7 +18,7 @@ <h2>Questions</h2>
1718
</div>
1819
<div class="col">
1920
<h2>Answers</h2>
20-
<div id="answer-images"></div>
21+
<div id="answers"></div>
2122
</div>
2223
</body>
2324
</html>

public/style.css

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,31 @@ textarea#question {
2020
margin-bottom: 1em;
2121
}
2222

23-
img {
23+
#answers > div {
2424
box-shadow: 0 0 0.5em #bbb;
2525
margin-bottom: 1.5em;
2626
}
2727

28+
#answers > div .question {
29+
color: orange;
30+
font-style: italic;
31+
padding: 5px;
32+
float: left;
33+
}
34+
35+
#answers > div .stat {
36+
color: #999;
37+
font-size: 0.8em;
38+
float: right;
39+
padding: 5px;
40+
}
41+
42+
#answers > div .answer {
43+
font-weight: bold;
44+
padding: 5px;
45+
clear: both;
46+
}
47+
2848
#status {
2949
color: orange;
3050
padding-left: 1em;

0 commit comments

Comments
 (0)