{"id":3617,"date":"2015-04-02T21:07:58","date_gmt":"2015-04-02T21:07:58","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3617"},"modified":"2015-04-02T21:13:25","modified_gmt":"2015-04-02T21:13:25","slug":"firebase-notes","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/firebase-notes\/","title":{"rendered":"Firebase Notes"},"content":{"rendered":"<hr \/>\n<p><strong><em>A powerful API to store and sync data in realtime.<\/em><\/strong><\/p>\n<h2>Steps<\/h2>\n<ol>\n<li><strong>Install<\/strong>\n<pre>&lt;script src='https:\/\/cdn.firebase.com\/js\/client\/2.2.1\/firebase.js'&gt;&lt;\/script&gt;<\/pre>\n<\/li>\n<li><strong>Reference<\/strong> (URL for your Firebase data)\n<pre>var myDataRef = new Firebase('https:\/\/xze3eg07ah9.firebaseio-demo.com\/');<\/pre>\n<\/li>\n<li>Write data with\u00a0<span class=\"code\">.set()<\/span> method on a Firebase reference (write an object too) &#8211; <span class=\"code\">myDataRef.set({name: name, text: text});<\/span> \u2026 In this example, when the object <span class=\"code\">{name: name, text: text}<\/span> is set, locations for name and text are automatically created as children of the location referenced by <span class=\"code\">myDataRef<\/span><\/li>\n<li>Firebase can support number, boolean, and string data types \u2014 the same as a normal JavaScript object<\/li>\n<li>Use <span class=\"code\">.push()<\/span> method to append to a list &#8211; Firebase lists use chronologically-ordered, globally-unique IDs as the keys for items&#8230; This means that many clients can add to the same list without conflict<\/li>\n<li>We need to tell Firebase to notify us when chat messages arrive. We do this by adding a callback to the list of chat messages using the <span class=\"code\">.on()<\/span> method, as shown below:\n<pre>myDataRef.on('child_added', function(snapshot) {\r\n    var message = snapshot.val();\r\n    displayChatMessage(message.name, message.text);\r\n});<\/pre>\n<\/li>\n<\/ol>\n<p>With Firebase, you always read data using callbacks. This allows us to guarantee that Firebase applications always update in real-time. Note that when your app starts, Firebase will call your callback for all existing chat messages and then continue to call it for any new messages that arrive. There&#8217;s intentionally no distinction between &#8220;initial&#8221; data and &#8220;new&#8221; data. This allows you to write your message handling logic once rather than having to handle these two cases separately.<\/p>\n<pre>&lt;html&gt;\r\n &lt;head&gt;\r\n   &lt;script src='https:\/\/cdn.firebase.com\/js\/client\/2.2.1\/firebase.js'&gt;&lt;\/script&gt;\r\n   &lt;script src='https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.1\/jquery.min.js'&gt;&lt;\/script&gt;\r\n &lt;\/head&gt;\r\n &lt;body&gt;\r\n   &lt;div id='messagesDiv'&gt;&lt;\/div&gt;\r\n   &lt;input type='text' id='nameInput' placeholder='Name'&gt;\r\n   &lt;input type='text' id='messageInput' placeholder='Message'&gt;\r\n   &lt;script&gt;\r\n     var myDataRef = new Firebase('https:\/\/xze3eg07ah9.firebaseio-demo.com\/');\r\n     $('#messageInput').keypress(function (e) {\r\n       if (e.keyCode == 13) {\r\n         var name = $('#nameInput').val();\r\n         var text = $('#messageInput').val();\r\n         myDataRef.push({name: name, text: text});\r\n         $('#messageInput').val('');\r\n       }\r\n     });\r\n     myDataRef.on('child_added', function(snapshot) {\r\n       var message = snapshot.val();\r\n       displayChatMessage(message.name, message.text);\r\n     });\r\n     function displayChatMessage(name, text) {\r\n       $('&lt;div\/&gt;').text(text).prepend($('&lt;em\/&gt;').text(name+': ')).appendTo($('#messagesDiv'));\r\n       $('#messagesDiv')[0].scrollTop = $('#messagesDiv')[0].scrollHeight;\r\n     };\r\n   &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.firebase.com\/how-it-works.html\" target=\"_blank\">Firebase<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A powerful API to store and sync data in realtime. Steps Install &lt;script src=&#8217;https:\/\/cdn.firebase.com\/js\/client\/2.2.1\/firebase.js&#8217;&gt;&lt;\/script&gt; Reference (URL for your Firebase data) var myDataRef = new Firebase(&#8216;https:\/\/xze3eg07ah9.firebaseio-demo.com\/&#8217;); Write data with\u00a0.set() method on a Firebase reference (write an object too) &#8211; myDataRef.set({name: name, text: text}); \u2026 In this example, when the object {name: name, text: text} is set, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[9,27,157],"class_list":["post-3617","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-javascript","tag-jquery","tag-tools"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3617"}],"version-history":[{"count":8,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3617\/revisions"}],"predecessor-version":[{"id":3625,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3617\/revisions\/3625"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}