Date post: | 28-Jan-2015 |
Category: |
Technology |
Upload: | kompozer |
View: | 126 times |
Download: | 2 times |
Node.js and WebSockets
A (very) short introduction
Andreas Kompanez
Montag, 26. April 2010
Node.jsJavaScript 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, synchronotherThing();
Montag, 26. April 2010
Evented?
Evented I/Ofile.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
// 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));
Module Example
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.jsvar 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
PollingMailmanClient Server
Montag, 26. April 2010
BidirectionalClient ServerTelephone
Montag, 26. April 2010
WS Communication details
Handshake
Messages
GET /test HTTP/1.1Upgrade: WebSocketConnection: UpgradeOrigin: http://localhost/testHost: localhostContent-Length: 0
HTTP/1.1 101 Web Socket Protocol HandshakeUpgrade: WebSocketConnection: UpgradeServer: Resin/4.0.2WebSocket-Location: ws://localhost/websocketWebSocket-Origin: http://localhost/testContent-Length: 0Date: Fri, 08 May 2009 09:51:31 GMT
\x00hello, world\xff
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