Remember to check out the documentation for restdb.io Pages and for restdb.io Querying. You can also view the master template source code.
{{#inherit "layout"}}
{{#block "head"}}
<title>restdb.io realtime</title>
<link rel="stylesheet" href="{{root}}/realtimedemo.css">
{{/block}}
{{#block "title"}}
<div class="col-md-12">
<h1>Realtime REST demo</h1>
</div>
{{/block}}
{{#block "content"}}
<div class="container realtimedemo">
<div class="well col-md-12">
Realtime improves user experience (UX) and the speed of communication.
RestDB.io lets your application subscribe to Data Events in realtime.
This example shows a chat application that visualises REST Data Events (PUT, POST, DELETE) and the executing code block.
To get started, <a target="quickstart" href="https://restdb.io/docs/quick-start">create a new database</a>, read the <a target="jsapi" href="https://restdb.io/docs/javascript-api">docs</a> and look at the Github <a target="github" href="https://github.com/RestDB/clientexamples/tree/master/restdb_io%20realtime%20chat">code example</a>.
<br><br>
Send a link to this page to your team/friends, start chatting, and watch the events propagate.
</div>
<div class="col-md-4">
<h4>Chat</h4>
<hr>
<input type="text" id="oneliner">
<small>Double click or swipe right to delete</small>
<div id="messages"></div>
</div>
<div class="col-md-8">
<h4>The code</h4>
<hr>
<pre id="post_block"><code class="javascript">// Called when new records are inserted into the database
db.chat.on('POST', function(err, mess) {
var line = $("<div>").text(mess.data.oneliner + " " + mess.data._id);
line.hide();
$("#messages").prepend(line);
line.toggle("scale")
});
</code></pre>
<pre id="put_block"><code class="javascript">// Called when a record is changed in the database
db.chat.on('PUT', function(err, mess) {
db.chat.find({"_id": mess.data}, {}, function(err, updatedline){
$("#"+mess.data).text(updatedline[0].oneliner);
});
});
</code></pre>
<pre id="delete_block"><code class="javascript">// Called when one or more record is deleted in the database
db.chat.on('DELETE', function(err, mess) {
_.each(mess.data, function(oneid){
$("#"+oneid).remove();
});
});
</code></pre>
<pre id="init_block"><code class="javascript">// The initial setup and connection the a realtime database
var apikey = "577ed0a645c8ac6e5b035fbe";
var db = null;
try {
db = new restdb(apikey, {realtime: true});
$("#status").text("Ok, got the api").addClass("online");
} catch (ex) {
$("#status").text("Error loading jsapi");
}
</code></pre>
<pre id="input_block"><code class="javascript">// Called when a user types something in the input field
$("#oneliner").keyup(function(e){
if(e.keyCode == 13)
{
var payload = {"oneliner": $(this).val()};
$(this).val("");
var newline = new db.chat(payload);
newline.save();
}
});
</code></pre>
<pre id="connect_block"><code class="javascript">// Called when a connection is established
db.on("CONNECT", function() {
$("#status").addClass("online").removeClass("offline").text("online");
$("#messages").empty();
db.chat.find({}, {"$max": 100,"$orderby": {"_created": -1}}, function(err, lines){
_.each(lines, function(aline){
$("#messages").append($("<div>").text(aline.oneliner));
});
})
});
</code></pre>
<pre id="disconnect_block"><code class="javascript">//Called when the connection is lost, unplug your wifi to see this
db.on("DISCONNECT", function() {
console.log("Realtime closed: ");
$("#status").addClass("online").addClass("offline").text("off");
});
</code></pre>
<pre id="recconnect_block"><code class="javascript">// Called after a lost connection is regained
db.on("RECONNECT", function() {
$("#status").addClass("online").removeClass("offline").text("Back again");
});
</code></pre>
</div>
</div>
{{/block}}
{{#block "scripts"}}
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.10.0/lodash.js"></script>
<!-- restdb.io libraries -->
<script src="https://realtimedemo-7c18.restdb.io/assets/js/eventsource.min.js"></script>
<script src="https://realtimedemo-7c18.restdb.io/rest/_jsapi.js"></script>
<script src="{{root}}/jquery.mobile.custom.min.js"></script>
<!-- code for this demo -->
<script src="{{root}}/realtimedemo.js"></script>
{{/block}}
{{/inherit}}