SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
responsive, real-time
with Backbone.js and WebSockets



   Andrei Cîmpean, @Andrei_Cimpean
Today, we are going to look at how we are
building a real-time, responsive JavaScript
client app.
For Google Chrome.
And IE8.
What is a real-time application ?
●   It enables users to receive information as soon
    as it is published

What is a responsive application ?
●   Its UI updates instantly, no matter what
Why do we need “real-time, responsive”
●   a client imports entries, packages them and
    puts them up on the market for another users
    to bid on and, if they win the auction, to pay
●   an app that behaves like a market
●   allows clients to sell and bid
●   time constrained user actions ( seconds )
What we needed to do
●   structure the market on the client app
●   communication mechanism
●   don't stress the server
●   support only Chrome and IE 8+ :|
●   make sure it works at least a full day without
    crashing or requiring a full page refresh
Technology decisions
●   Backbone.js + plugins – for structure
●   CometD – for communication
Structure
Backbone.js + plugins
Backbone.js ( http://backbonejs.org/ )
BB is a JavaScript library with a RESTful JSON interface, and is
based on the model–view–presenter (MVP) application design
paradigm. It provides:
●   models with key-value binding and custom events
●   collections with a rich API of enumerable functions
●   views with declarative event handling
●   routing system with History API support
Backbone.js ( http://backbonejs.org/ )
// model definition
var Package = Backbone.Model.extend({
 doSomething: function() { … }
});
// collection definition
var Repository = Backbone.Collection.extend({
 model: Package
});
Backbone.js ( http://backbonejs.org/ )
●   Entry and Package as Models,
●   Entries and Repository ( fancy for packages) as
    Collections
●   add an Entries collection in Package to satisfy
    relationship
Backbone.js Views
var PackageWithBid = Backbone.View.extend({
 events: {
      "click .button.accept": "accept",
      "click .button.reject": "reject"
 },
 render: function () { ... }
 ...
});
new PackageWithBid({ model: repository.first() }).render().el; // DOM element
Backbone.js Views
●   views can be binded to model or collection
    events
●   managing a large number of nested views
    can be a pain
Backbone.js LayoutManager
( https://github.com/tbranyen/backbone.layoutmanager )

●   a better way to structure
    views
●   easy to set up
●   render() is now managed
●   gain beforeRender() and
    afterRender()
●   manages cleanup
Nesting Views
var ViewBids = Backbone.View.extend({
 manage: true, // needed by layout manager for our approach
 beforeRender: function() {
      this.collection.each(function() {
       this.insertView(new PackageWithBid());
      }, this);
 },
 serialize: function() {
      return { package: this.model };
 }
});
Why Backbone.js ?
●   Backbone.js was chosen because it is small, very
    light; The entire source code can be read with
    ease ( ~ 1500 LOC )
●   good documentation
●   a big community with a lot of plugins, some
    really good
Communication
WebSockets
“It has to be a bit more complicated.”
            Internet Explorer Dev Team
WebSockets ( http://www.websocket.org/ )
●   a full-duplex single socket connection over
    which messages can be sent between client and
    server
●   doesn't work in Internet Explorer
WebSockets
Sorting out IE8+
●   any browser with Flash can support WebSocket
    using a web-socket-js shim/polyfill
    –   flash leaks memory
●   fallback system: long-polling vs flash
    –   flash leaks memory
●   use a “wrapper” that enables fallback
CometD ( http://cometd.org/ )
●   scalable HTTP-based event routing bus that uses a
    Ajax Push technology pattern known as Comet
●   provides meta-channels for error messages
●   provides channels that can be used to filter content
    update by subscribing to them:
    var sub = socket.subscribe('/foo/bar/', function() { ... });
    cometd.unsubscribe(sub);
Connecting to a CometD server
function connect(path) {
    window.socket.configure({ url: path });


    window.socket.addListener('/meta/subscribe',metaSubscribe);
    window.socket.addListener('/meta/handshake', metaHandshake);
    window.socket.addListener('/meta/connect', metaConnect);


    return window.socket.handshake({
     ext: { authentication: { user: username, sessionid: sessionid } }
    });
}
Architecture decisions
●   use comet channels
    –   an update channel, where bids on the client's
        repository are sent
    –   an offer channel, where new packages are
        sent for other clients to bid on
Architecture decisions
●   pass-through REST-like server:
    –   GET returns appropriate responses
    –   POST, PUT, DELETE
        ●   200 OK - if server is not dead
        ●   confirmation on a channel will be received
            as soon as possible
Optimism and responsiveness
●   we are optimists
●   each action that would require a confirmation is
    actually displayed as if it was a success immediately
●   if the confirmation message says that something
    failed, we just display a notification and revert. Most
    of the time this is not required.
Reverting optimism
●   when the action is performed, after the server
    call we replace old views with ones representing
    the new state ( one property )
●   if the confirmation invalidates the action, we
    restore the previous state and re-render all
●   this approach guards almost all the actions a
    user can perform in the application
Reverting optimism
●   actions that change more than just an order
    state, first create a snapshot of itself
●   if the confirmation invalidates the action, we
    restore the snapshot and re-render all
●   for these, we render the view representing the
    new state but block them with a loading
    animation
So, basically
●   be optimistic when rendering but prepared to
    raise alarms
●   always use ;

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction To JavaScript
Introduction To JavaScriptIntroduction To JavaScript
Introduction To JavaScriptReema
 
jQuery - Chapter 1 - Introduction
 jQuery - Chapter 1 - Introduction jQuery - Chapter 1 - Introduction
jQuery - Chapter 1 - IntroductionWebStackAcademy
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewVisual Engineering
 
Agile JavaScript Testing
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript TestingScott Becker
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationAndrew Rota
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
 
Workshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsWorkshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsVisual Engineering
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101ygv2000
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module PatternsNicholas Jansma
 
A Closer Look At React Native
A Closer Look At React NativeA Closer Look At React Native
A Closer Look At React NativeIan Wang
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 
Getting started with node JS
Getting started with node JSGetting started with node JS
Getting started with node JSHamdi Hmidi
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptMarlon Jamera
 
An Introduction To Testing In AngularJS Applications
An Introduction To Testing In AngularJS Applications An Introduction To Testing In AngularJS Applications
An Introduction To Testing In AngularJS Applications Rohan Chandane
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareVisual Engineering
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScriptYakov Fain
 

Was ist angesagt? (20)

Introduction To JavaScript
Introduction To JavaScriptIntroduction To JavaScript
Introduction To JavaScript
 
jQuery - Chapter 1 - Introduction
 jQuery - Chapter 1 - Introduction jQuery - Chapter 1 - Introduction
jQuery - Chapter 1 - Introduction
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General Overview
 
Agile JavaScript Testing
Agile JavaScript TestingAgile JavaScript Testing
Agile JavaScript Testing
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
Workshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsWorkshop 3: JavaScript build tools
Workshop 3: JavaScript build tools
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
Javascript Module Patterns
Javascript Module PatternsJavascript Module Patterns
Javascript Module Patterns
 
A Closer Look At React Native
A Closer Look At React NativeA Closer Look At React Native
A Closer Look At React Native
 
Angularjs
AngularjsAngularjs
Angularjs
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Getting started with node JS
Getting started with node JSGetting started with node JS
Getting started with node JS
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Java script
Java scriptJava script
Java script
 
An Introduction To Testing In AngularJS Applications
An Introduction To Testing In AngularJS Applications An Introduction To Testing In AngularJS Applications
An Introduction To Testing In AngularJS Applications
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux Middleware
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 

Ähnlich wie An approach to responsive, realtime with Backbone.js and WebSockets

Developing Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's GuideDeveloping Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's GuideMohanraj Thirumoorthy
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'sAntônio Roberto Silva
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slidesDavid Barreto
 
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)Brian Brazil
 
Google app-engine-cloudcamplagos2011
Google app-engine-cloudcamplagos2011Google app-engine-cloudcamplagos2011
Google app-engine-cloudcamplagos2011Opevel
 
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_OSDC 2018 | From Monolith to Microservices by Paul Puschmann_
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_NETWAYS
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendVlad Fedosov
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteorSapna Upreti
 
"Service Worker: Let Your Web App Feel Like a Native "
"Service Worker: Let Your Web App Feel Like a Native ""Service Worker: Let Your Web App Feel Like a Native "
"Service Worker: Let Your Web App Feel Like a Native "FDConf
 
OSGi Cloud Ecosystems - David Bosschaert
OSGi Cloud Ecosystems - David BosschaertOSGi Cloud Ecosystems - David Bosschaert
OSGi Cloud Ecosystems - David Bosschaertmfrancis
 
Sprint 46 review
Sprint 46 reviewSprint 46 review
Sprint 46 reviewManageIQ
 
How Ansible Tower and Prometheus can help automate continuous deployments
How Ansible Tower and Prometheus can help automate continuous deployments How Ansible Tower and Prometheus can help automate continuous deployments
How Ansible Tower and Prometheus can help automate continuous deployments Roger Tanner
 
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoasRest web service_with_spring_hateoas
Rest web service_with_spring_hateoasZeid Hassan
 
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...JSFestUA
 
Meetup 2022 - APIs with Quarkus.pdf
Meetup 2022 - APIs with Quarkus.pdfMeetup 2022 - APIs with Quarkus.pdf
Meetup 2022 - APIs with Quarkus.pdfLuca Mattia Ferrari
 
Building Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devicesBuilding Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devicesWindows Developer
 

Ähnlich wie An approach to responsive, realtime with Backbone.js and WebSockets (20)

Developing Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's GuideDeveloping Microservices using Spring - Beginner's Guide
Developing Microservices using Spring - Beginner's Guide
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API's
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slides
 
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)
Monitoring Kubernetes with Prometheus (Kubernetes Ireland, 2016)
 
Google app-engine-cloudcamplagos2011
Google app-engine-cloudcamplagos2011Google app-engine-cloudcamplagos2011
Google app-engine-cloudcamplagos2011
 
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_OSDC 2018 | From Monolith to Microservices by Paul Puschmann_
OSDC 2018 | From Monolith to Microservices by Paul Puschmann_
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontend
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteor
 
"Service Worker: Let Your Web App Feel Like a Native "
"Service Worker: Let Your Web App Feel Like a Native ""Service Worker: Let Your Web App Feel Like a Native "
"Service Worker: Let Your Web App Feel Like a Native "
 
OSGi Cloud Ecosystems - David Bosschaert
OSGi Cloud Ecosystems - David BosschaertOSGi Cloud Ecosystems - David Bosschaert
OSGi Cloud Ecosystems - David Bosschaert
 
Sprint 46 review
Sprint 46 reviewSprint 46 review
Sprint 46 review
 
Express node js
Express node jsExpress node js
Express node js
 
Google App Engine
Google App EngineGoogle App Engine
Google App Engine
 
Google Cloud Platform
Google Cloud Platform Google Cloud Platform
Google Cloud Platform
 
Mean stack Magics
Mean stack MagicsMean stack Magics
Mean stack Magics
 
How Ansible Tower and Prometheus can help automate continuous deployments
How Ansible Tower and Prometheus can help automate continuous deployments How Ansible Tower and Prometheus can help automate continuous deployments
How Ansible Tower and Prometheus can help automate continuous deployments
 
Rest web service_with_spring_hateoas
Rest web service_with_spring_hateoasRest web service_with_spring_hateoas
Rest web service_with_spring_hateoas
 
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
 
Meetup 2022 - APIs with Quarkus.pdf
Meetup 2022 - APIs with Quarkus.pdfMeetup 2022 - APIs with Quarkus.pdf
Meetup 2022 - APIs with Quarkus.pdf
 
Building Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devicesBuilding Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devices
 

Kürzlich hochgeladen

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Kürzlich hochgeladen (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

An approach to responsive, realtime with Backbone.js and WebSockets

  • 1. responsive, real-time with Backbone.js and WebSockets Andrei Cîmpean, @Andrei_Cimpean
  • 2. Today, we are going to look at how we are building a real-time, responsive JavaScript client app. For Google Chrome. And IE8.
  • 3. What is a real-time application ? ● It enables users to receive information as soon as it is published What is a responsive application ? ● Its UI updates instantly, no matter what
  • 4. Why do we need “real-time, responsive” ● a client imports entries, packages them and puts them up on the market for another users to bid on and, if they win the auction, to pay ● an app that behaves like a market ● allows clients to sell and bid ● time constrained user actions ( seconds )
  • 5. What we needed to do ● structure the market on the client app ● communication mechanism ● don't stress the server ● support only Chrome and IE 8+ :| ● make sure it works at least a full day without crashing or requiring a full page refresh
  • 6. Technology decisions ● Backbone.js + plugins – for structure ● CometD – for communication
  • 8. Backbone.js ( http://backbonejs.org/ ) BB is a JavaScript library with a RESTful JSON interface, and is based on the model–view–presenter (MVP) application design paradigm. It provides: ● models with key-value binding and custom events ● collections with a rich API of enumerable functions ● views with declarative event handling ● routing system with History API support
  • 9. Backbone.js ( http://backbonejs.org/ ) // model definition var Package = Backbone.Model.extend({ doSomething: function() { … } }); // collection definition var Repository = Backbone.Collection.extend({ model: Package });
  • 10. Backbone.js ( http://backbonejs.org/ ) ● Entry and Package as Models, ● Entries and Repository ( fancy for packages) as Collections ● add an Entries collection in Package to satisfy relationship
  • 11. Backbone.js Views var PackageWithBid = Backbone.View.extend({ events: { "click .button.accept": "accept", "click .button.reject": "reject" }, render: function () { ... } ... }); new PackageWithBid({ model: repository.first() }).render().el; // DOM element
  • 12. Backbone.js Views ● views can be binded to model or collection events ● managing a large number of nested views can be a pain
  • 13. Backbone.js LayoutManager ( https://github.com/tbranyen/backbone.layoutmanager ) ● a better way to structure views ● easy to set up ● render() is now managed ● gain beforeRender() and afterRender() ● manages cleanup
  • 14. Nesting Views var ViewBids = Backbone.View.extend({ manage: true, // needed by layout manager for our approach beforeRender: function() { this.collection.each(function() { this.insertView(new PackageWithBid()); }, this); }, serialize: function() { return { package: this.model }; } });
  • 15. Why Backbone.js ? ● Backbone.js was chosen because it is small, very light; The entire source code can be read with ease ( ~ 1500 LOC ) ● good documentation ● a big community with a lot of plugins, some really good
  • 17. “It has to be a bit more complicated.” Internet Explorer Dev Team
  • 18. WebSockets ( http://www.websocket.org/ ) ● a full-duplex single socket connection over which messages can be sent between client and server ● doesn't work in Internet Explorer
  • 20. Sorting out IE8+ ● any browser with Flash can support WebSocket using a web-socket-js shim/polyfill – flash leaks memory ● fallback system: long-polling vs flash – flash leaks memory ● use a “wrapper” that enables fallback
  • 21. CometD ( http://cometd.org/ ) ● scalable HTTP-based event routing bus that uses a Ajax Push technology pattern known as Comet ● provides meta-channels for error messages ● provides channels that can be used to filter content update by subscribing to them: var sub = socket.subscribe('/foo/bar/', function() { ... }); cometd.unsubscribe(sub);
  • 22. Connecting to a CometD server function connect(path) { window.socket.configure({ url: path }); window.socket.addListener('/meta/subscribe',metaSubscribe); window.socket.addListener('/meta/handshake', metaHandshake); window.socket.addListener('/meta/connect', metaConnect); return window.socket.handshake({ ext: { authentication: { user: username, sessionid: sessionid } } }); }
  • 23. Architecture decisions ● use comet channels – an update channel, where bids on the client's repository are sent – an offer channel, where new packages are sent for other clients to bid on
  • 24. Architecture decisions ● pass-through REST-like server: – GET returns appropriate responses – POST, PUT, DELETE ● 200 OK - if server is not dead ● confirmation on a channel will be received as soon as possible
  • 25. Optimism and responsiveness ● we are optimists ● each action that would require a confirmation is actually displayed as if it was a success immediately ● if the confirmation message says that something failed, we just display a notification and revert. Most of the time this is not required.
  • 26. Reverting optimism ● when the action is performed, after the server call we replace old views with ones representing the new state ( one property ) ● if the confirmation invalidates the action, we restore the previous state and re-render all ● this approach guards almost all the actions a user can perform in the application
  • 27. Reverting optimism ● actions that change more than just an order state, first create a snapshot of itself ● if the confirmation invalidates the action, we restore the snapshot and re-render all ● for these, we render the view representing the new state but block them with a loading animation
  • 28. So, basically ● be optimistic when rendering but prepared to raise alarms ● always use ;