SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
Node.js and
                           WebSockets
                         A (very) short introduction

                              Andreas Kompanez




Montag, 26. April 2010
Node.js
                         JavaScript Framework

                         Server-side

                         Uses V8

                         Evented and non-blocking

                         CommonJS

                         Uses ECMAScript 5


Montag, 26. April 2010
Node.js


                         Created by Ryan Dahl

                         ~8000 lines of C/C++ and 2000 lines
                         JavaScript

                         http://nodejs.org/




Montag, 26. April 2010
Evented?

                         Old (blocking) school:
                         <?php
                         $content = file_get_contents("/some/huge/file");
                         doThings($content); // Waiting, synchron
                         otherThing();




Montag, 26. April 2010
Evented?

                         Evented I/O
                         file.read("/some/huge/file", function(data) {
                             // called after data is read
                             doThings(data);
                         });
                         otherThing(); // execute immediately;




Montag, 26. April 2010
Benefits

                         Asynchronous programming

                         Event-loops instead of threads

                         Non-blocking

                         1 Thread (No context switching etc.)




Montag, 26. April 2010
CommonJS




Montag, 26. April 2010
CommonJS

                         A collection/library of standards

                           Modules

                           Binary

                           File system

                           and many more!



Montag, 26. April 2010
CommonJS Modules

                         There should be a function require

                         There should be a var called exports




Montag, 26. April 2010
Module Example
                         // math.js module
                         exports.multiply = function(a, b) {
                             return a * b;
                         }




                         // Some other file, using math.js
                         //
                         var math = require('./math');
                         var sys = require('sys');

                         sys.puts(math.multiply(12, 12));




Montag, 26. April 2010
Google V8 JavaScript
                      Engine
                         It’s a VM!

                         Developed by Google

                         The team lead is Lars Bak (Sun’s Java
                         VM & Smalltalk VM)

                         No JIT, compiled to Assembler



Montag, 26. April 2010
The 6+ lines http
                                server
                         // httpserver.js
                         // Usage: node httpserver.js
                         var sys = require("sys"),
                             http = require("http");

                         http.createServer(function(request, response) {
                           var headers = { "Content-Type": "text/plain" };
                           response.sendHeader(200, headers);
                           response.write("Hello, World!n");
                           response.close();
                         }).listen(8000);

                         sys.puts("Running at http://127.0.0.1:8000/");




Montag, 26. April 2010
WebSockets




Montag, 26. April 2010
WebSockets

                         A HTML5 standard

                         Allows bidirectional communications

                         Less overhead than HTTP/AJAX (less
                         headers)

                         Cross domain



Montag, 26. April 2010
Polling
                   Client    Mailman   Server




Montag, 26. April 2010
Bidirectional
                   Client   Telephone   Server




Montag, 26. April 2010
WS Communication
                              details
                         Handshake
                         GET /test HTTP/1.1              HTTP/1.1 101 Web Socket Protocol Handshake
                         Upgrade: WebSocket              Upgrade: WebSocket
                         Connection: Upgrade             Connection: Upgrade
                         Origin: http://localhost/test   Server: Resin/4.0.2
                         Host: localhost                 WebSocket-Location: ws://localhost/websocket
                         Content-Length: 0               WebSocket-Origin: http://localhost/test
                                                         Content-Length: 0
                                                         Date: Fri, 08 May 2009 09:51:31 GMT



                         Messages
                         x00hello, worldxff




Montag, 26. April 2010
WebSockets Client-
                                side
                   if (("WebSocket" in window)) {
                       var ws = new WebSocket("ws://localhost:8080/");

                         ws.onmessage = function(evt) {
                             // Receive
                         };
                         ws.onopen = function(evt) {
                             //
                             ws.send('Oh hai');
                         };
                         ws.onclose = function(evt) {
                             //
                         };
                         ws.onerror = function(evt) {
                             //
                         };
                         ws.close();
                   }

Montag, 26. April 2010
Demo!




Montag, 26. April 2010

Más contenido relacionado

La actualidad más candente

MySQL Slow Query log Monitoring using Beats & ELK
MySQL Slow Query log Monitoring using Beats & ELKMySQL Slow Query log Monitoring using Beats & ELK
MySQL Slow Query log Monitoring using Beats & ELKI Goo Lee
 
Crate Shared Nothing Web Backends - Web Backend Meetup May 2014
Crate Shared Nothing Web Backends - Web Backend Meetup May 2014Crate Shared Nothing Web Backends - Web Backend Meetup May 2014
Crate Shared Nothing Web Backends - Web Backend Meetup May 2014Matthias Wahl
 
CloudStack and cloud-init
CloudStack and cloud-initCloudStack and cloud-init
CloudStack and cloud-initMarcusS13
 
Advanced Replication
Advanced ReplicationAdvanced Replication
Advanced ReplicationMongoDB
 
Hydra - Getting Started
Hydra - Getting StartedHydra - Getting Started
Hydra - Getting Startedabramsm
 
Re: 제로부터시작하는텐서플로우
Re: 제로부터시작하는텐서플로우Re: 제로부터시작하는텐서플로우
Re: 제로부터시작하는텐서플로우Mario Cho
 
Cocoon Blocks ApacheCon US 2005
Cocoon Blocks ApacheCon US 2005Cocoon Blocks ApacheCon US 2005
Cocoon Blocks ApacheCon US 2005Daniel Fagerstrom
 
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014Kevin Lo
 
Scale11x lxc talk
Scale11x lxc talkScale11x lxc talk
Scale11x lxc talkdotCloud
 
RapidInsight for OpenNMS
RapidInsight for OpenNMSRapidInsight for OpenNMS
RapidInsight for OpenNMSmberkay
 
Archlinux install
Archlinux installArchlinux install
Archlinux installsambismo
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linuxrowiebornia
 
Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)MongoDB
 
Using MMS to Build New Environments
Using MMS to Build New EnvironmentsUsing MMS to Build New Environments
Using MMS to Build New EnvironmentsMongoDB
 

La actualidad más candente (20)

MySQL Slow Query log Monitoring using Beats & ELK
MySQL Slow Query log Monitoring using Beats & ELKMySQL Slow Query log Monitoring using Beats & ELK
MySQL Slow Query log Monitoring using Beats & ELK
 
Crate Shared Nothing Web Backends - Web Backend Meetup May 2014
Crate Shared Nothing Web Backends - Web Backend Meetup May 2014Crate Shared Nothing Web Backends - Web Backend Meetup May 2014
Crate Shared Nothing Web Backends - Web Backend Meetup May 2014
 
Os Balog
Os BalogOs Balog
Os Balog
 
CloudStack and cloud-init
CloudStack and cloud-initCloudStack and cloud-init
CloudStack and cloud-init
 
LSA2 - 02 Namespaces
LSA2 - 02  NamespacesLSA2 - 02  Namespaces
LSA2 - 02 Namespaces
 
Advanced Replication
Advanced ReplicationAdvanced Replication
Advanced Replication
 
Hydra - Getting Started
Hydra - Getting StartedHydra - Getting Started
Hydra - Getting Started
 
Re: 제로부터시작하는텐서플로우
Re: 제로부터시작하는텐서플로우Re: 제로부터시작하는텐서플로우
Re: 제로부터시작하는텐서플로우
 
LSA2 - PostgreSQL
LSA2 - PostgreSQLLSA2 - PostgreSQL
LSA2 - PostgreSQL
 
Cocoon Blocks ApacheCon US 2005
Cocoon Blocks ApacheCon US 2005Cocoon Blocks ApacheCon US 2005
Cocoon Blocks ApacheCon US 2005
 
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
 
Dockerの準備
Dockerの準備Dockerの準備
Dockerの準備
 
Scale11x lxc talk
Scale11x lxc talkScale11x lxc talk
Scale11x lxc talk
 
RapidInsight for OpenNMS
RapidInsight for OpenNMSRapidInsight for OpenNMS
RapidInsight for OpenNMS
 
Archlinux install
Archlinux installArchlinux install
Archlinux install
 
Introduction to-linux
Introduction to-linuxIntroduction to-linux
Introduction to-linux
 
Bower introduction
Bower introductionBower introduction
Bower introduction
 
Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)Replica Sets (NYC NoSQL Meetup)
Replica Sets (NYC NoSQL Meetup)
 
Using MMS to Build New Environments
Using MMS to Build New EnvironmentsUsing MMS to Build New Environments
Using MMS to Build New Environments
 
【Unity】Scriptable object 入門と活用例
【Unity】Scriptable object 入門と活用例【Unity】Scriptable object 入門と活用例
【Unity】Scriptable object 入門と活用例
 

Similar a Node.js and websockets intro

Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevFelix Geisendörfer
 
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)CODE WHITE GmbH
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)Felix Geisendörfer
 
SenchaLabs Connect & Express
SenchaLabs Connect & ExpressSenchaLabs Connect & Express
SenchaLabs Connect & ExpressTim Caswell
 
Web Standards @ Opera Talk Oslo
Web Standards @ Opera Talk OsloWeb Standards @ Opera Talk Oslo
Web Standards @ Opera Talk OsloZi Bin Cheah
 
Real Time Web - What's that for?
Real Time Web - What's that for?Real Time Web - What's that for?
Real Time Web - What's that for?Martyn Loughran
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSocketsGonzalo Ayuso
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineRicardo Silva
 
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
 
Comet with node.js and V8
Comet with node.js and V8Comet with node.js and V8
Comet with node.js and V8amix3k
 
Open End To End Js Stack
Open End To End Js StackOpen End To End Js Stack
Open End To End Js StackSkills Matter
 
Bootstrap of Node.js Core (OpenJS Collaborator Summit Berlin 2019)
Bootstrap of Node.js Core (OpenJS Collaborator Summit Berlin 2019)Bootstrap of Node.js Core (OpenJS Collaborator Summit Berlin 2019)
Bootstrap of Node.js Core (OpenJS Collaborator Summit Berlin 2019)Igalia
 
Web Crawling with NodeJS
Web Crawling with NodeJSWeb Crawling with NodeJS
Web Crawling with NodeJSSylvain Zimmer
 
Java Deserialization Vulnerabilities - The Forgotten Bug Class
Java Deserialization Vulnerabilities - The Forgotten Bug ClassJava Deserialization Vulnerabilities - The Forgotten Bug Class
Java Deserialization Vulnerabilities - The Forgotten Bug ClassCODE WHITE GmbH
 
Watir Presentation Sumanth Krishna. A
Watir Presentation   Sumanth Krishna. AWatir Presentation   Sumanth Krishna. A
Watir Presentation Sumanth Krishna. ASumanth krishna
 

Similar a Node.js and websockets intro (20)

Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
How we're building Wercker
How we're building WerckerHow we're building Wercker
How we're building Wercker
 
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
Java Deserialization Vulnerabilities - The Forgotten Bug Class (RuhrSec Edition)
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)
 
Nodejs Intro
Nodejs IntroNodejs Intro
Nodejs Intro
 
SenchaLabs Connect & Express
SenchaLabs Connect & ExpressSenchaLabs Connect & Express
SenchaLabs Connect & Express
 
Web Standards @ Opera Talk Oslo
Web Standards @ Opera Talk OsloWeb Standards @ Opera Talk Oslo
Web Standards @ Opera Talk Oslo
 
Real Time Web - What's that for?
Real Time Web - What's that for?Real Time Web - What's that for?
Real Time Web - What's that for?
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
Node.js and Ruby
Node.js and RubyNode.js and Ruby
Node.js and Ruby
 
XQuery Design Patterns
XQuery Design PatternsXQuery Design Patterns
XQuery Design Patterns
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
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
 
Comet with node.js and V8
Comet with node.js and V8Comet with node.js and V8
Comet with node.js and V8
 
Open End To End Js Stack
Open End To End Js StackOpen End To End Js Stack
Open End To End Js Stack
 
Bootstrap of Node.js Core (OpenJS Collaborator Summit Berlin 2019)
Bootstrap of Node.js Core (OpenJS Collaborator Summit Berlin 2019)Bootstrap of Node.js Core (OpenJS Collaborator Summit Berlin 2019)
Bootstrap of Node.js Core (OpenJS Collaborator Summit Berlin 2019)
 
Web Crawling with NodeJS
Web Crawling with NodeJSWeb Crawling with NodeJS
Web Crawling with NodeJS
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
Java Deserialization Vulnerabilities - The Forgotten Bug Class
Java Deserialization Vulnerabilities - The Forgotten Bug ClassJava Deserialization Vulnerabilities - The Forgotten Bug Class
Java Deserialization Vulnerabilities - The Forgotten Bug Class
 
Watir Presentation Sumanth Krishna. A
Watir Presentation   Sumanth Krishna. AWatir Presentation   Sumanth Krishna. A
Watir Presentation Sumanth Krishna. A
 

Último

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
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...Miguel Araújo
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
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
 
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...DianaGray10
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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
 
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...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Node.js and websockets intro

  • 1. Node.js and WebSockets A (very) short introduction Andreas Kompanez Montag, 26. April 2010
  • 2. Node.js JavaScript Framework Server-side Uses V8 Evented and non-blocking CommonJS Uses ECMAScript 5 Montag, 26. April 2010
  • 3. Node.js Created by Ryan Dahl ~8000 lines of C/C++ and 2000 lines JavaScript http://nodejs.org/ Montag, 26. April 2010
  • 4. Evented? Old (blocking) school: <?php $content = file_get_contents("/some/huge/file"); doThings($content); // Waiting, synchron otherThing(); Montag, 26. April 2010
  • 5. Evented? Evented I/O file.read("/some/huge/file", function(data) { // called after data is read doThings(data); }); otherThing(); // execute immediately; Montag, 26. April 2010
  • 6. Benefits Asynchronous programming Event-loops instead of threads Non-blocking 1 Thread (No context switching etc.) Montag, 26. April 2010
  • 8. CommonJS A collection/library of standards Modules Binary File system and many more! Montag, 26. April 2010
  • 9. CommonJS Modules There should be a function require There should be a var called exports Montag, 26. April 2010
  • 10. Module Example // math.js module exports.multiply = function(a, b) { return a * b; } // Some other file, using math.js // var math = require('./math'); var sys = require('sys'); sys.puts(math.multiply(12, 12)); Montag, 26. April 2010
  • 11. Google V8 JavaScript Engine It’s a VM! Developed by Google The team lead is Lars Bak (Sun’s Java VM & Smalltalk VM) No JIT, compiled to Assembler Montag, 26. April 2010
  • 12. The 6+ lines http server // httpserver.js // Usage: node httpserver.js var sys = require("sys"), http = require("http"); http.createServer(function(request, response) { var headers = { "Content-Type": "text/plain" }; response.sendHeader(200, headers); response.write("Hello, World!n"); response.close(); }).listen(8000); sys.puts("Running at http://127.0.0.1:8000/"); Montag, 26. April 2010
  • 14. WebSockets A HTML5 standard Allows bidirectional communications Less overhead than HTTP/AJAX (less headers) Cross domain Montag, 26. April 2010
  • 15. Polling Client Mailman Server Montag, 26. April 2010
  • 16. Bidirectional Client Telephone Server Montag, 26. April 2010
  • 17. WS Communication details Handshake GET /test HTTP/1.1 HTTP/1.1 101 Web Socket Protocol Handshake Upgrade: WebSocket Upgrade: WebSocket Connection: Upgrade Connection: Upgrade Origin: http://localhost/test Server: Resin/4.0.2 Host: localhost WebSocket-Location: ws://localhost/websocket Content-Length: 0 WebSocket-Origin: http://localhost/test Content-Length: 0 Date: Fri, 08 May 2009 09:51:31 GMT Messages x00hello, worldxff Montag, 26. April 2010
  • 18. WebSockets Client- side if (("WebSocket" in window)) { var ws = new WebSocket("ws://localhost:8080/"); ws.onmessage = function(evt) { // Receive }; ws.onopen = function(evt) { // ws.send('Oh hai'); }; ws.onclose = function(evt) { // }; ws.onerror = function(evt) { // }; ws.close(); } Montag, 26. April 2010