Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 4 - Plug in Ably client-side, update UI, auth
- Loading branch information
1 parent
db6fa91
commit cf84468
Showing
4 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,48 @@ | ||
$(function() { | ||
var $actionButton = $('#action-btn'); | ||
/* Set up realtime library and authenticate using token issued from server */ | ||
var ably = new Ably.Realtime({ authUrl: '/auth' }); | ||
var filteredChannel = ably.channels.get('neutrino:filtered'); | ||
var publishChannel = ably.channels.get('neutrino:raw'); | ||
|
||
var $output = $('#output'), | ||
$status = $('#status'), | ||
$actionButton = $('#action-btn'), | ||
$text = $('#text'); | ||
|
||
/* Subscribe to filtered text published on this channel */ | ||
filteredChannel.subscribe(function(message) { | ||
var $filtered = $('<div class="filtered">').text(message.data.filtered); | ||
var $stat = $('<div class="stat">').text("Neutrino took " + message.data.neutrinoTime + "ms"); | ||
$output.prepend($('<div>').append($stat).append($filtered)); | ||
}); | ||
|
||
$actionButton.on('click', function() { | ||
alert("Not yet implemented"); | ||
var text = $text.val(); | ||
if (text.replace(' ') != '') { | ||
/* Publish text to the Ably channel so that the queue worker receives it via queues */ | ||
publishChannel.publish('text', text, function(err) { | ||
if (err) { | ||
showStatus('Failed to publish text!'); | ||
$text.val(text); | ||
return; | ||
} | ||
clearStatus(); | ||
}); | ||
showStatus('Sending unfiltered bad text...'); | ||
$text.val(''); | ||
} | ||
}); | ||
|
||
ably.connection.on('connecting', function() { showStatus('Connecting to Ably...'); }); | ||
ably.connection.on('connected', function() { clearStatus(); }); | ||
ably.connection.on('disconnected', function() { showStatus('Disconnected from Ably...'); }); | ||
ably.connection.on('suspended', function() { showStatus('Disconnected from Ably for a while...'); }); | ||
|
||
function showStatus(text) { | ||
$status.text(text).show(); | ||
} | ||
|
||
function clearStatus() { | ||
$status.fadeOut(750); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters