Node.js and
                           WebSockets
                         A (very) short introduction

                  ...
Node.js
                         JavaScript Framework

                         Server-side

                         Uses...
Node.js


                         Created by Ryan Dahl

                         ~8000 lines of C/C++ and 2000 lines
    ...
Evented?

                         Old (blocking) school:
                         <?php
                         $content...
Evented?

                         Evented I/O
                         file.read("/some/huge/file", function(data) {
    ...
Benefits

                         Asynchronous programming

                         Event-loops instead of threads

    ...
CommonJS




Montag, 26. April 2010
CommonJS

                         A collection/library of standards

                           Modules

                ...
CommonJS Modules

                         There should be a function require

                         There should be a ...
Module Example
                         // math.js module
                         exports.multiply = function(a, b) {
   ...
Google V8 JavaScript
                      Engine
                         It’s a VM!

                         Developed ...
The 6+ lines http
                                server
                         // httpserver.js
                       ...
WebSockets




Montag, 26. April 2010
WebSockets

                         A HTML5 standard

                         Allows bidirectional communications

     ...
Polling
                   Client    Mailman   Server




Montag, 26. April 2010
Bidirectional
                   Client   Telephone   Server




Montag, 26. April 2010
WS Communication
                              details
                         Handshake
                         GET /te...
WebSockets Client-
                                side
                   if (("WebSocket" in window)) {
                ...
Demo!




Montag, 26. April 2010
Upcoming SlideShare
Loading in...5
×

Node.js and websockets intro

27,269

Published on

A short intro about node.js and websockets. You can find the demostration at http://github.com/kompozer/tweamer

Published in: Technology
0 Comments
15 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
27,269
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
393
Comments
0
Likes
15
Embeds 0
No embeds

No notes for slide

Node.js and websockets intro

  1. 1. Node.js and WebSockets A (very) short introduction Andreas Kompanez Montag, 26. April 2010
  2. 2. Node.js JavaScript Framework Server-side Uses V8 Evented and non-blocking CommonJS Uses ECMAScript 5 Montag, 26. April 2010
  3. 3. Node.js Created by Ryan Dahl ~8000 lines of C/C++ and 2000 lines JavaScript http://nodejs.org/ Montag, 26. April 2010
  4. 4. Evented? Old (blocking) school: <?php $content = file_get_contents("/some/huge/file"); doThings($content); // Waiting, synchron otherThing(); Montag, 26. April 2010
  5. 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. 6. Benefits Asynchronous programming Event-loops instead of threads Non-blocking 1 Thread (No context switching etc.) Montag, 26. April 2010
  7. 7. CommonJS Montag, 26. April 2010
  8. 8. CommonJS A collection/library of standards Modules Binary File system and many more! Montag, 26. April 2010
  9. 9. CommonJS Modules There should be a function require There should be a var called exports Montag, 26. April 2010
  10. 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. 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. 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
  13. 13. WebSockets Montag, 26. April 2010
  14. 14. WebSockets A HTML5 standard Allows bidirectional communications Less overhead than HTTP/AJAX (less headers) Cross domain Montag, 26. April 2010
  15. 15. Polling Client Mailman Server Montag, 26. April 2010
  16. 16. Bidirectional Client Telephone Server Montag, 26. April 2010
  17. 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. 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
  19. 19. Demo! Montag, 26. April 2010
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×