SlideShare a Scribd company logo
1 of 24
Download to read offline
Building Realtime
              Singlepage Apps
                      @HenrikJoreteg
                      (hurray for Twitter)




Friday, May 6, 2011
The web is in
                             DIRECT
                      competition with native
                               apps


Friday, May 6, 2011
Stop supporting
                      crappy browsers.

                        Just STOP!

Friday, May 6, 2011
focus on providing
                         compelling
                         experiences
               (with the latest technologies)


Friday, May 6, 2011
Adoption is directly
                      related to how easy
                       it is to tinker with.


Friday, May 6, 2011
Socket.io is to realtime
                what jQuery is to AJAX



Friday, May 6, 2011
Quick look at
                       Socket.io


Friday, May 6, 2011
Server
                      var http = require('http'),  
                          io = require('socket.io');

                      server = http.createServer(function(req, res){
                       // your normal server code
                       res.writeHead(200, {'Content-Type': 'text/html'});
                       res.end('<h1>Hello world</h1>');
                      });
                      server.listen(80);
                       
                      // socket.io
                      var socket = io.listen(server);
                      socket.on('connection', function(client){
                        // new client is here!
                        client.on('message', function(){ … })
                        client.on('disconnect', function(){ … })
                      });




Friday, May 6, 2011
Client
                      <script src="http://{node_server_url}/socket.io/socket.io.js"></script>
                      <script>
                        var socket = new io.Socket({node_server_url});
                        socket.connect();
                        socket.on('connect', function(){ … })
                        socket.on('message', function(){ … })
                        socket.on('disconnect', function(){ … })
                      </script>




Friday, May 6, 2011
It’s all about state



Friday, May 6, 2011
Quick Look at
                       Backbone.js


Friday, May 6, 2011
Backbone gives you Observable
               Models & Collections
          var App = Backbone.Model.extend({
            initialize: function () {
               this.bind(‘change:myProperty’, _(this.writeIt).bind(this));
            },
            writeIt: function () {
               console.log(“my prop is: “ + this.get(‘myProperty’));
            }
          });

          app = new App();
          app.set({myProperty: true}); // outputs: “my prop is: true”




Friday, May 6, 2011
Views just listen to model changes
                var AppView = Backbone.Model.extend({
                  initialize: function () {
                     this.model.bind(‘change’, _(this.render).bind(this));
                  },
                  render: function () {
                     this.el.html(ich.app(this.model.toJSON()));
                     return this;
                  }
                });

                var app = new App(); // init our model
                var appView = new AppView({ model: app, el:
                document.body }); // init our view

                appView.render();
                app.set({myProperty: true}); // will re-render the whole app


Friday, May 6, 2011
Quick look at
                        Capsule


Friday, May 6, 2011
Backbone.js
                             +
                      model nesting
                             +
                       serialization
                             +
                      event bubbling
Friday, May 6, 2011
Models shared by Server + Client
           (function () {

               ... detect and set up environment for CommonJS or browser here

               // Main app model
               exports.AppModel = Capsule.Model.extend({
                 type: 'app',
                 initialize: function (spec) {
                   this.register();
                   this.addChildCollection('members', exports.Members);
                   this.addChildModel('activityLog', exports.ActivityLogPage);
                 }
               });

             // other models
             exports.Members = ...
           })();



Friday, May 6, 2011
Server-side
      socket.on('connection', function (client) {
        var app, sessionId;

          // this is split out so we have a reference to it
          function sendClientChanges(changes) { client.send(changes); }

          client.on('message', function(message){
            var model, collection;

              switch (message.event) {
                case 'session':
                   ...
                   // Here you'd fetch your user from DB based on sessionid
                   // you'd also get the corresponding app state that they should have access to.
                   // `require` your shared models file and inflate or instantiate your root app model
                   // Then grab whatever else you need and send the intial state to the client.
                   client.send({
                     event: 'initial',
                     app: app.xport()
                   });

                      // bind to the root `publish` events to send any changes to this client
                      app.bind('publish', sendClientChanges);
                      ...



Friday, May 6, 2011
Clientside
                      $(function () {
                        var app = window.app = new AppModel();
                        window.socket = new io.Socket();

                        // get and send our session cookie
                        socket.on('connect', function() {
                          socket.send({
                            event: 'session',
                            cookie: $.cookie('&!')
                          });
                        });

                        socket.on(‘message’, ...
                      });



Friday, May 6, 2011
Clientside cont...
                      socket.on('message', function (msg) {
                        switch (msg.event) {
                          case 'initial':
                            //import app state
                            app.mport(msg.app);
                            // init and render our root view
                            view = window.view = new AppView({
                              el: $('body'),
                              model: app
                            }).render();
                            break;
                          case 'change':
                            app.modelGetter(msg.id).set(msg.data);
                            break;

                            ... other cases for `add`, `remove` etc.
                        }
                      });


Friday, May 6, 2011
Friday, May 6, 2011
Friday, May 6, 2011
Friday, May 6, 2011
Challenges
                      • Partially shared state
                         Hint, sync everything, have your
                         view render what’s relevant
                      • Scaling
                      • Providing external APIs




Friday, May 6, 2011
Thank you!
                         Helpful Resources:
           me on twitter: @HenrikJoreteg (hit me up for early invite to &!)
           Backbone.js: http://documentcloud.github.com/backbone/
           Capsule.js: https://github.com/andyet/capsule
           App Testing: http://funcunit.com,
           http://mwbrooks.github.com/dominator.js/



Friday, May 6, 2011

More Related Content

What's hot

Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 

What's hot (12)

Wicket 6
Wicket 6Wicket 6
Wicket 6
 
Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
Dependency injection with koin
Dependency injection with koinDependency injection with koin
Dependency injection with koin
 
9.Spring DI_4
9.Spring DI_49.Spring DI_4
9.Spring DI_4
 
ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2
 
Technozaure - Angular2
Technozaure - Angular2Technozaure - Angular2
Technozaure - Angular2
 
[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법[FEConf Korea 2017]Angular 컴포넌트 대화법
[FEConf Korea 2017]Angular 컴포넌트 대화법
 
Learning Appcelerator® Alloy™
Learning Appcelerator® Alloy™Learning Appcelerator® Alloy™
Learning Appcelerator® Alloy™
 
Angular js architecture (v1.4.8)
Angular js architecture (v1.4.8)Angular js architecture (v1.4.8)
Angular js architecture (v1.4.8)
 
Explaination of angular
Explaination of angularExplaination of angular
Explaination of angular
 
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
React ES5 to ES6 | React ES5 vs ES6 | React Tutorial for Beginners | React on...
 

Similar to Node conf - building realtime webapps

Introducing Applitude: Simple Module Management
Introducing Applitude: Simple Module ManagementIntroducing Applitude: Simple Module Management
Introducing Applitude: Simple Module Management
Eric Hamilton
 
Magento's Imagine eCommerce Conference 2011 - Unit Testing with Magento
Magento's Imagine eCommerce Conference 2011 - Unit Testing with MagentoMagento's Imagine eCommerce Conference 2011 - Unit Testing with Magento
Magento's Imagine eCommerce Conference 2011 - Unit Testing with Magento
MagentoImagine
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Java EE 7: Boosting Productivity and Embracing HTML5
Java EE 7: Boosting Productivity and Embracing HTML5Java EE 7: Boosting Productivity and Embracing HTML5
Java EE 7: Boosting Productivity and Embracing HTML5
Arun Gupta
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
Ravi Mone
 
YUI3 Modules
YUI3 ModulesYUI3 Modules
YUI3 Modules
a_pipkin
 
Helma / RingoJS – Vienna.js Minitalk
Helma / RingoJS – Vienna.js MinitalkHelma / RingoJS – Vienna.js Minitalk
Helma / RingoJS – Vienna.js Minitalk
Philipp Naderer
 

Similar to Node conf - building realtime webapps (20)

Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Nashville Symfony Functional Testing
Nashville Symfony Functional TestingNashville Symfony Functional Testing
Nashville Symfony Functional Testing
 
Introducing Applitude: Simple Module Management
Introducing Applitude: Simple Module ManagementIntroducing Applitude: Simple Module Management
Introducing Applitude: Simple Module Management
 
Magento Imagine eCommerce Conference - February 2011 - Unit Testing with Magento
Magento Imagine eCommerce Conference - February 2011 - Unit Testing with MagentoMagento Imagine eCommerce Conference - February 2011 - Unit Testing with Magento
Magento Imagine eCommerce Conference - February 2011 - Unit Testing with Magento
 
Magento's Imagine eCommerce Conference 2011 - Unit Testing with Magento
Magento's Imagine eCommerce Conference 2011 - Unit Testing with MagentoMagento's Imagine eCommerce Conference 2011 - Unit Testing with Magento
Magento's Imagine eCommerce Conference 2011 - Unit Testing with Magento
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
Android Architecture Components
Android Architecture ComponentsAndroid Architecture Components
Android Architecture Components
 
Backbone js
Backbone jsBackbone js
Backbone js
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Pushing the Web: Interesting things to Know
Pushing the Web: Interesting things to KnowPushing the Web: Interesting things to Know
Pushing the Web: Interesting things to Know
 
Java EE 7 Platform: Boosting Productivity and Embracing HTML5 - Arun Gupta (R...
Java EE 7 Platform: Boosting Productivity and Embracing HTML5 - Arun Gupta (R...Java EE 7 Platform: Boosting Productivity and Embracing HTML5 - Arun Gupta (R...
Java EE 7 Platform: Boosting Productivity and Embracing HTML5 - Arun Gupta (R...
 
Java EE 7: Boosting Productivity and Embracing HTML5
Java EE 7: Boosting Productivity and Embracing HTML5Java EE 7: Boosting Productivity and Embracing HTML5
Java EE 7: Boosting Productivity and Embracing HTML5
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
 
Real Time App with Node.js
Real Time App with Node.jsReal Time App with Node.js
Real Time App with Node.js
 
YUI3 Modules
YUI3 ModulesYUI3 Modules
YUI3 Modules
 
Vaadin 7 by Joonas Lehtinen
Vaadin 7 by Joonas LehtinenVaadin 7 by Joonas Lehtinen
Vaadin 7 by Joonas Lehtinen
 
Web components
Web componentsWeb components
Web components
 
Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)Building Isomorphic Apps (JSConf.Asia 2014)
Building Isomorphic Apps (JSConf.Asia 2014)
 
Helma / RingoJS – Vienna.js Minitalk
Helma / RingoJS – Vienna.js MinitalkHelma / RingoJS – Vienna.js Minitalk
Helma / RingoJS – Vienna.js Minitalk
 
A gently introduction to AngularJS
A gently introduction to AngularJSA gently introduction to AngularJS
A gently introduction to AngularJS
 

Recently uploaded

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
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...
 
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
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Node conf - building realtime webapps

  • 1. Building Realtime Singlepage Apps @HenrikJoreteg (hurray for Twitter) Friday, May 6, 2011
  • 2. The web is in DIRECT competition with native apps Friday, May 6, 2011
  • 3. Stop supporting crappy browsers. Just STOP! Friday, May 6, 2011
  • 4. focus on providing compelling experiences (with the latest technologies) Friday, May 6, 2011
  • 5. Adoption is directly related to how easy it is to tinker with. Friday, May 6, 2011
  • 6. Socket.io is to realtime what jQuery is to AJAX Friday, May 6, 2011
  • 7. Quick look at Socket.io Friday, May 6, 2011
  • 8. Server var http = require('http'),       io = require('socket.io'); server = http.createServer(function(req, res){  // your normal server code  res.writeHead(200, {'Content-Type': 'text/html'});  res.end('<h1>Hello world</h1>'); }); server.listen(80);   // socket.io var socket = io.listen(server); socket.on('connection', function(client){   // new client is here!   client.on('message', function(){ … })   client.on('disconnect', function(){ … }) }); Friday, May 6, 2011
  • 9. Client <script src="http://{node_server_url}/socket.io/socket.io.js"></script> <script>   var socket = new io.Socket({node_server_url});   socket.connect();   socket.on('connect', function(){ … })   socket.on('message', function(){ … })   socket.on('disconnect', function(){ … }) </script> Friday, May 6, 2011
  • 10. It’s all about state Friday, May 6, 2011
  • 11. Quick Look at Backbone.js Friday, May 6, 2011
  • 12. Backbone gives you Observable Models & Collections var App = Backbone.Model.extend({ initialize: function () { this.bind(‘change:myProperty’, _(this.writeIt).bind(this)); }, writeIt: function () { console.log(“my prop is: “ + this.get(‘myProperty’)); } }); app = new App(); app.set({myProperty: true}); // outputs: “my prop is: true” Friday, May 6, 2011
  • 13. Views just listen to model changes var AppView = Backbone.Model.extend({ initialize: function () { this.model.bind(‘change’, _(this.render).bind(this)); }, render: function () { this.el.html(ich.app(this.model.toJSON())); return this; } }); var app = new App(); // init our model var appView = new AppView({ model: app, el: document.body }); // init our view appView.render(); app.set({myProperty: true}); // will re-render the whole app Friday, May 6, 2011
  • 14. Quick look at Capsule Friday, May 6, 2011
  • 15. Backbone.js + model nesting + serialization + event bubbling Friday, May 6, 2011
  • 16. Models shared by Server + Client (function () { ... detect and set up environment for CommonJS or browser here // Main app model exports.AppModel = Capsule.Model.extend({ type: 'app', initialize: function (spec) { this.register(); this.addChildCollection('members', exports.Members); this.addChildModel('activityLog', exports.ActivityLogPage); } }); // other models exports.Members = ... })(); Friday, May 6, 2011
  • 17. Server-side socket.on('connection', function (client) { var app, sessionId; // this is split out so we have a reference to it function sendClientChanges(changes) { client.send(changes); } client.on('message', function(message){ var model, collection; switch (message.event) { case 'session': ... // Here you'd fetch your user from DB based on sessionid // you'd also get the corresponding app state that they should have access to. // `require` your shared models file and inflate or instantiate your root app model // Then grab whatever else you need and send the intial state to the client. client.send({ event: 'initial', app: app.xport() }); // bind to the root `publish` events to send any changes to this client app.bind('publish', sendClientChanges); ... Friday, May 6, 2011
  • 18. Clientside $(function () { var app = window.app = new AppModel(); window.socket = new io.Socket(); // get and send our session cookie socket.on('connect', function() { socket.send({ event: 'session', cookie: $.cookie('&!') }); }); socket.on(‘message’, ... }); Friday, May 6, 2011
  • 19. Clientside cont... socket.on('message', function (msg) { switch (msg.event) { case 'initial': //import app state app.mport(msg.app); // init and render our root view view = window.view = new AppView({ el: $('body'), model: app }).render(); break; case 'change': app.modelGetter(msg.id).set(msg.data); break; ... other cases for `add`, `remove` etc. } }); Friday, May 6, 2011
  • 23. Challenges • Partially shared state Hint, sync everything, have your view render what’s relevant • Scaling • Providing external APIs Friday, May 6, 2011
  • 24. Thank you! Helpful Resources: me on twitter: @HenrikJoreteg (hit me up for early invite to &!) Backbone.js: http://documentcloud.github.com/backbone/ Capsule.js: https://github.com/andyet/capsule App Testing: http://funcunit.com, http://mwbrooks.github.com/dominator.js/ Friday, May 6, 2011