Created
January 25, 2016 17:17
-
-
Save anmolagrwl/1a57690c3c0d0ef1d9ce to your computer and use it in GitHub Desktop.
PubNub - RethinkDB Demo
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>RethinkDB PubNub</title> | |
<script src="index.js" charset="utf-8"></script> | |
<script src="https://cdn.pubnub.com/pubnub-3.7.13.min.js"></script> | |
<script src="jquery.js" charset="utf-8"></script> | |
<style> | |
#book{ | |
margin: auto; | |
display: block; | |
text-align: center; | |
} | |
.available{ | |
font-size: 50px; | |
} | |
/* #number{ | |
font-size: 50px; | |
color: #006600; | |
} */ | |
</style> | |
</head> | |
<body> | |
<h1>Book Availablity</h1> | |
<div id="book"> | |
<span class="available">Harry Potter Series - <span id="availability_status"></span></span> | |
</div> | |
</body> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
var pubnub = PUBNUB({ | |
subscribe_key : 'demo' | |
}); | |
pubnub.subscribe({ | |
channel : "rethinkdb-pubnub", // Subscribing to PubNub's channel | |
message : function(message){ | |
console.log(message); | |
$("#availability_status")[0].innerHTML = message.status; // Embeding status inside the message that's coming through channel and putting it in HTML | |
} | |
}) | |
}); | |
</script> | |
</html> |
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
var r = require('rethinkdb'); // Initialise rethinkdb instance | |
var pubnub = require("pubnub")({ // Initialise pubnub instance | |
subscribe_key: 'demo', // always required | |
publish_key: 'demo' // only required if publishing | |
}); | |
// Establish a connection | |
var connection = null; | |
r.connect( {host: 'localhost', port: 28015}, function(err, conn) { | |
if (err) throw err; | |
connection = conn; | |
// Get a book | |
r.table('books').filter(r.row('name').eq("Harry Potter Series")) // Get a book with name 'Harry Potter' | |
.changes() // Look for any changes happening to this record and keeps returning it | |
.run(connection, function(err, cursor) { // run the above query on this connection | |
if (err) throw err; | |
cursor.each(function(err, result) { // If there is no error, it returns a cursor, which is collection of JSON objects | |
if (err) throw err; | |
pubnub.publish({ //publishing the updated Book's status through PubNub, in 'rethinkdb-pubnub' channel | |
channel: "rethinkdb-pubnub", | |
message: {"status" : result.new_val.availability_status}, //this is the message payload we are sending | |
// result contains a new value and the old value of this record. This is due to .changes() method. | |
callback: function(m){console.log(m)} | |
}); | |
console.log({"status" : result.new_val.availability_status}); | |
}); | |
}); | |
}) | |
// r.table('books').insert({name: "Harry Potter Series", author: "J.K.Rowling", availability_status: "Available"}) | |
// r.table('books').filter(r.row("name").eq("Harry Potter Series")).update({availability_status: "Booked"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment