PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://github.com/colincarr/pubnub-backbone
Markdown | 93 lines | 64 code | 29 blank | 0 comment | 0 complexity | 101c98a0071e9bddf547ef1c972abfdb MD5 | raw file
  1. PubNub Backbone
  2. ========
  3. This is a framework for [Backbone](http://backbonejs.org) integration with [PubNub](http://pubnub.com). It works by syncronizing all create, update, and delete action between your Backbone collections. This gives real-time updating between Backbone collections and models on different clients.
  4. PubNub is a real-time network that provides the core tools to build real-time applications and scale them globally. Backbone gives structure to web applications with bindings and events for web application data. Combining the two gives a rich application experience for multi-user applications such as collaboritive editing, chat rooms, and even multiplayer games.
  5. Check out the demo here: [http://pubnub.github.io/backbone/](http://pubnub.github.io/backbone/)
  6. # Installation
  7. You can install the package using bower and `bower install pubnub-backbone` or by cloning this repository. Install the PubNub library along with this one in your html file like so, replacing the *'s with the PubNub version you want to use:
  8. ```html
  9. <script src="http://cdn.pubnub.com/pubnub.min.js"></script>
  10. <script src="/path/to/backbone-pubnub.min.js"></script>
  11. ```
  12. # Getting Started
  13. The framework works by syncronizing all create, update, and delete events between collections on all clients. This can be done manually through a plugin that hooks into the Backbone.sync method. This will give you a local collection that can be sync'ed with a remote collection that is kept up to date with PubNub. The other option is to use the custom classes provided to create a collection or model that is always in sync.
  14. # Initialize PubNub
  15. Once both libraries are installed and ready to go the first step is to create a shared instance of the PubNub library. This will allow your real-time collections and models to communicate across the PubNub network.
  16. ```javascript
  17. var pubnub = PUBNUB.init({
  18. publish_key: 'demo',
  19. subscribe_key: 'demo'
  20. });
  21. ```
  22. # Backbone.PubNub.Collection
  23. This collection takes the pubnub instance and a name and then publishes all create, update, and delete methods across clients using PubNub. The `name` property is used to generate a unique channel name so collections do not collide with each other. As a warning, with the same name will update each other regardless of what class they come from.
  24. ```javascript
  25. var MyCollection = Backbone.PubNub.Collection.extend({
  26. name: 'MyCollection', // Used to namespace the updates to this collection
  27. pubnub: pubnub // A global instance of PubNub
  28. });
  29. var myCollection = new MyCollection();
  30. ```
  31. # Backbone.sync
  32. The Backbone.sync method allows you to have a regular collection with a remote store, much like the LocalStorage module from the original Backbone Todos demo. This allows you to take updates from other clients when you want them and not automatically.
  33. ```javascript
  34. var MyCollection = Backbone.Collection.extend({
  35. pubnub: new Backbone.PubNub(pubnub, "MyCollection") // A PubNub plugin to make the collection real-time
  36. });
  37. var myCollection = new MyCollection();
  38. ```
  39. # Backbone.PubNub.Model
  40. This will create a model that is updated in real-time across all instances. This works very similarly to the collection in that it publishes all changes across the PubNub network.
  41. ```javascript
  42. var MyModel = Backbone.PubNub.Model.extend({
  43. name: 'MyModel',
  44. pubnub: pubnub
  45. });
  46. var myModel = new MyModel();
  47. ```
  48. # License
  49. The MIT License (MIT)
  50. Copyright (c) 2013 PubNub
  51. Permission is hereby granted, free of charge, to any person obtaining a copy of
  52. this software and associated documentation files (the "Software"), to deal in
  53. the Software without restriction, including without limitation the rights to
  54. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  55. the Software, and to permit persons to whom the Software is furnished to do so,
  56. subject to the following conditions:
  57. The above copyright notice and this permission notice shall be included in all
  58. copies or substantial portions of the Software.
  59. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  60. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  61. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  62. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  63. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  64. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.