SlideShare a Scribd company logo
1 of 16
Download to read offline
Live Collections
with Backbone and Faye
TROUPE
@suprememoocow
@mydigitalself
@WeAreTroupe | github.com/Troupe | trou.pe
Why are we talking about this?
A real-time drop-in replacement
for Backbone collections
Technology
Faye (http://faye.jcoglan.com)
Backbone.js (http://backbonejs.org)
Mongoose (http://mongoosejs.com)
Node.js & Express
MongoDB
Baucis (http://github.com/wprl/baucis)
Why Faye?
var Faye   = require('faye'),
    server = new Faye.NodeAdapter({mount: '/'});
server.listen(8000);
var client = new Faye.Client('http://localhost:8000/');
client.subscribe('/messages', function(message) {
  alert('Got a message: ' + message.text);
});
client.publish('/messages', {
  text: 'Hello world'
});
1
2
3
Start a server
Create a client
Send messages
REST (Client->Server) FAYE (Broadcast)OPERATION
Using a REST-like scheme for Pub/Sub
GET /api/todos/ client.subscribe(‘/api/todos/’, ...)
Create POST /api/todos/ { method:“POST”, body: {...} }
Update PUT /api/todos/id { method:“PUT”, body: {...} }
Delete DELETE /api/todos/id { method:“DELETE”, body: {...} }
http://faye-live-demo.jit.su/
function notifyClients(method, model, url) {
" fayeServer.getClient().publish(url, {
" " method: method,
" " body: model.toJSON()
" });
}
mongooseUtils.attachListenersToSchema(todoSchema, {
" onCreate: function(model, next) {
" " notifyClients('POST', model, '/api/todos');
" " next();
},
onUpdate: function(model, next) {
" " notifyClients('PUT', model, '/api/todos');
" " next();
},
onRemove: function(model) {
" " notifyClients('DELETE', model, '/api/todos');
}
});
Publishing events on the server via Mongoose middleware
var LiveCollection = Backbone.Collection.extend({
constructor: function(models, options) {
...
this.subscription = fayeClient.subscribe(this.url, this.fayeEvent);
},
fayeEvent: function(message) {
var method = message.method;
var body = message.body;
switch(method) {
case 'POST':
this._createEvent(body);
break;
case 'PUT':
this._updateEvent(body);
break;
case 'DELETE':
this._removeEvent(body);
break;
...
}
},
Listening to events on the client (LiveCollection base class)
Beware! Here there be dragons!
3.
beer.
Views need to respond to non UI
changes to the collection.
Address in code.We recommend using Marionette
(http://marionettejs.com/)
Ordering of events isn’t always what
you’d expect.
Faye events can be received by the client before the REST
operation returns. Need to handle this situation in code.
You need to consider security for your
websockets.
We use Bayeux extensions to authenticate on handshake and
subscribe messages.
Those dodgy websockets...
Various approaches including awake from sleep detection,
application-level pings. Can discuss over beer.
https://github.com/Troupe/faye-live-demo
Thank you
Oh, and we’re hiring!
trou.pe/join-us

More Related Content

What's hot

What mom never told you about bundle configurations - Symfony Live Paris 2012
What mom never told you about bundle configurations - Symfony Live Paris 2012What mom never told you about bundle configurations - Symfony Live Paris 2012
What mom never told you about bundle configurations - Symfony Live Paris 2012D
 
Socket.io under the hood
Socket.io under the hoodSocket.io under the hood
Socket.io under the hoodHaokang Den
 
What Is Async, How Does It Work, And When Should I Use It?
What Is Async, How Does It Work, And When Should I Use It?What Is Async, How Does It Work, And When Should I Use It?
What Is Async, How Does It Work, And When Should I Use It?emptysquare
 
asyncio community, one year later
asyncio community, one year laterasyncio community, one year later
asyncio community, one year laterVictor Stinner
 
JRuby最新事情@札幌
JRuby最新事情@札幌JRuby最新事情@札幌
JRuby最新事情@札幌Naoto Takai
 
[Js hcm] Deploying node.js with Forever.js and nginx
[Js hcm] Deploying node.js with Forever.js and nginx[Js hcm] Deploying node.js with Forever.js and nginx
[Js hcm] Deploying node.js with Forever.js and nginxNicolas Embleton
 
[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell Folks[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell FolksFunctional Thursday
 
api analytics redis bigquery. Lrug
api analytics redis bigquery. Lrugapi analytics redis bigquery. Lrug
api analytics redis bigquery. Lrugjavier ramirez
 
Deployment with Fabric
Deployment with FabricDeployment with Fabric
Deployment with Fabricandymccurdy
 
Fabric workshop(1) - (MOSG)
Fabric workshop(1) - (MOSG)Fabric workshop(1) - (MOSG)
Fabric workshop(1) - (MOSG)Soshi Nemoto
 
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonApache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonMasahiro Nagano
 
Scaling up task processing with Celery
Scaling up task processing with CeleryScaling up task processing with Celery
Scaling up task processing with CeleryNicolas Grasset
 
Meteorjs - Futuristic web framework
Meteorjs - Futuristic web frameworkMeteorjs - Futuristic web framework
Meteorjs - Futuristic web frameworkTomáš Hromník
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvarsSam Marley-Jarrett
 
Puppet Camp DC 2014: Keynote
Puppet Camp DC 2014: KeynotePuppet Camp DC 2014: Keynote
Puppet Camp DC 2014: KeynotePuppet
 
Building and Testing Puppet with Docker
Building and Testing Puppet with DockerBuilding and Testing Puppet with Docker
Building and Testing Puppet with Dockercarlaasouza
 
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015Masahiro Nagano
 
Pythonic Deployment with Fabric 0.9
Pythonic Deployment with Fabric 0.9Pythonic Deployment with Fabric 0.9
Pythonic Deployment with Fabric 0.9Corey Oordt
 

What's hot (20)

What mom never told you about bundle configurations - Symfony Live Paris 2012
What mom never told you about bundle configurations - Symfony Live Paris 2012What mom never told you about bundle configurations - Symfony Live Paris 2012
What mom never told you about bundle configurations - Symfony Live Paris 2012
 
aiohttp intro
aiohttp introaiohttp intro
aiohttp intro
 
Bundler
BundlerBundler
Bundler
 
Socket.io under the hood
Socket.io under the hoodSocket.io under the hood
Socket.io under the hood
 
What Is Async, How Does It Work, And When Should I Use It?
What Is Async, How Does It Work, And When Should I Use It?What Is Async, How Does It Work, And When Should I Use It?
What Is Async, How Does It Work, And When Should I Use It?
 
asyncio community, one year later
asyncio community, one year laterasyncio community, one year later
asyncio community, one year later
 
JRuby最新事情@札幌
JRuby最新事情@札幌JRuby最新事情@札幌
JRuby最新事情@札幌
 
[Js hcm] Deploying node.js with Forever.js and nginx
[Js hcm] Deploying node.js with Forever.js and nginx[Js hcm] Deploying node.js with Forever.js and nginx
[Js hcm] Deploying node.js with Forever.js and nginx
 
[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell Folks[FT-8][banacorn] Socket.IO for Haskell Folks
[FT-8][banacorn] Socket.IO for Haskell Folks
 
api analytics redis bigquery. Lrug
api analytics redis bigquery. Lrugapi analytics redis bigquery. Lrug
api analytics redis bigquery. Lrug
 
Deployment with Fabric
Deployment with FabricDeployment with Fabric
Deployment with Fabric
 
Fabric workshop(1) - (MOSG)
Fabric workshop(1) - (MOSG)Fabric workshop(1) - (MOSG)
Fabric workshop(1) - (MOSG)
 
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-ThonApache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
Apache::LogFormat::Compiler YAPC::Asia 2013 Tokyo LT-Thon
 
Scaling up task processing with Celery
Scaling up task processing with CeleryScaling up task processing with Celery
Scaling up task processing with Celery
 
Meteorjs - Futuristic web framework
Meteorjs - Futuristic web frameworkMeteorjs - Futuristic web framework
Meteorjs - Futuristic web framework
 
Symfony finally swiped right on envvars
Symfony finally swiped right on envvarsSymfony finally swiped right on envvars
Symfony finally swiped right on envvars
 
Puppet Camp DC 2014: Keynote
Puppet Camp DC 2014: KeynotePuppet Camp DC 2014: Keynote
Puppet Camp DC 2014: Keynote
 
Building and Testing Puppet with Docker
Building and Testing Puppet with DockerBuilding and Testing Puppet with Docker
Building and Testing Puppet with Docker
 
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015Rhebok, High Performance Rack Handler / Rubykaigi 2015
Rhebok, High Performance Rack Handler / Rubykaigi 2015
 
Pythonic Deployment with Fabric 0.9
Pythonic Deployment with Fabric 0.9Pythonic Deployment with Fabric 0.9
Pythonic Deployment with Fabric 0.9
 

Similar to Troupe lnug live collections

Netty - anfix tech&beers
Netty - anfix tech&beersNetty - anfix tech&beers
Netty - anfix tech&beersjorgecarabias
 
Building Ruby on Rails apps on Windows Azure (MIX 2010 at Last Vegas))
Building Ruby on Rails apps on Windows Azure (MIX 2010 at Last Vegas))Building Ruby on Rails apps on Windows Azure (MIX 2010 at Last Vegas))
Building Ruby on Rails apps on Windows Azure (MIX 2010 at Last Vegas))Sriram Krishnan
 
HTML5 vs Silverlight
HTML5 vs SilverlightHTML5 vs Silverlight
HTML5 vs SilverlightMatt Casto
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsAdrien Guéret
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rackdanwrong
 
Real-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.ioReal-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.ioRick Copeland
 
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 202010 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020Matt Raible
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsMarcus Frödin
 
Plack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversPlack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversTatsuhiko Miyagawa
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaYevgeniy Brikman
 
Ruby MVC from scratch with Rack
Ruby MVC from scratch with RackRuby MVC from scratch with Rack
Ruby MVC from scratch with RackDonSchado
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 

Similar to Troupe lnug live collections (20)

Netty - anfix tech&beers
Netty - anfix tech&beersNetty - anfix tech&beers
Netty - anfix tech&beers
 
NodeJS "Web en tiempo real"
NodeJS "Web en tiempo real"NodeJS "Web en tiempo real"
NodeJS "Web en tiempo real"
 
Building Ruby on Rails apps on Windows Azure (MIX 2010 at Last Vegas))
Building Ruby on Rails apps on Windows Azure (MIX 2010 at Last Vegas))Building Ruby on Rails apps on Windows Azure (MIX 2010 at Last Vegas))
Building Ruby on Rails apps on Windows Azure (MIX 2010 at Last Vegas))
 
HTML5 vs Silverlight
HTML5 vs SilverlightHTML5 vs Silverlight
HTML5 vs Silverlight
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rack
 
Real-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.ioReal-Time Python Web: Gevent and Socket.io
Real-Time Python Web: Gevent and Socket.io
 
Rack
RackRack
Rack
 
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 202010 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
10 Excellent Ways to Secure Spring Boot Applications - Okta Webinar 2020
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
Plack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and serversPlack perl superglue for web frameworks and servers
Plack perl superglue for web frameworks and servers
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
WebSockets with Spring 4
WebSockets with Spring 4WebSockets with Spring 4
WebSockets with Spring 4
 
Ruby MVC from scratch with Rack
Ruby MVC from scratch with RackRuby MVC from scratch with Rack
Ruby MVC from scratch with Rack
 
Aio...whatever
Aio...whateverAio...whatever
Aio...whatever
 
WebSockets in JEE 7
WebSockets in JEE 7WebSockets in JEE 7
WebSockets in JEE 7
 
The HTML5 WebSocket API
The HTML5 WebSocket APIThe HTML5 WebSocket API
The HTML5 WebSocket API
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
About Node.js
About Node.jsAbout Node.js
About Node.js
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 

Troupe lnug live collections

  • 3. Why are we talking about this?
  • 4. A real-time drop-in replacement for Backbone collections
  • 5. Technology Faye (http://faye.jcoglan.com) Backbone.js (http://backbonejs.org) Mongoose (http://mongoosejs.com) Node.js & Express MongoDB Baucis (http://github.com/wprl/baucis)
  • 6.
  • 7.
  • 9. var Faye   = require('faye'),     server = new Faye.NodeAdapter({mount: '/'}); server.listen(8000); var client = new Faye.Client('http://localhost:8000/'); client.subscribe('/messages', function(message) {   alert('Got a message: ' + message.text); }); client.publish('/messages', {   text: 'Hello world' }); 1 2 3 Start a server Create a client Send messages
  • 10. REST (Client->Server) FAYE (Broadcast)OPERATION Using a REST-like scheme for Pub/Sub GET /api/todos/ client.subscribe(‘/api/todos/’, ...) Create POST /api/todos/ { method:“POST”, body: {...} } Update PUT /api/todos/id { method:“PUT”, body: {...} } Delete DELETE /api/todos/id { method:“DELETE”, body: {...} }
  • 12. function notifyClients(method, model, url) { " fayeServer.getClient().publish(url, { " " method: method, " " body: model.toJSON() " }); } mongooseUtils.attachListenersToSchema(todoSchema, { " onCreate: function(model, next) { " " notifyClients('POST', model, '/api/todos'); " " next(); }, onUpdate: function(model, next) { " " notifyClients('PUT', model, '/api/todos'); " " next(); }, onRemove: function(model) { " " notifyClients('DELETE', model, '/api/todos'); } }); Publishing events on the server via Mongoose middleware
  • 13. var LiveCollection = Backbone.Collection.extend({ constructor: function(models, options) { ... this.subscription = fayeClient.subscribe(this.url, this.fayeEvent); }, fayeEvent: function(message) { var method = message.method; var body = message.body; switch(method) { case 'POST': this._createEvent(body); break; case 'PUT': this._updateEvent(body); break; case 'DELETE': this._removeEvent(body); break; ... } }, Listening to events on the client (LiveCollection base class)
  • 14. Beware! Here there be dragons! 3. beer. Views need to respond to non UI changes to the collection. Address in code.We recommend using Marionette (http://marionettejs.com/) Ordering of events isn’t always what you’d expect. Faye events can be received by the client before the REST operation returns. Need to handle this situation in code. You need to consider security for your websockets. We use Bayeux extensions to authenticate on handshake and subscribe messages. Those dodgy websockets... Various approaches including awake from sleep detection, application-level pings. Can discuss over beer.
  • 16. Thank you Oh, and we’re hiring! trou.pe/join-us