19
Node.js and WebSockets A (very) short introduction Andreas Kompanez Montag, 26. April 2010

Node.js and websockets intro

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Node.js and websockets intro

Node.js and WebSockets

A (very) short introduction

Andreas Kompanez

Montag, 26. April 2010

Page 2: Node.js and websockets intro

Node.jsJavaScript Framework

Server-side

Uses V8

Evented and non-blocking

CommonJS

Uses ECMAScript 5

Montag, 26. April 2010

Page 3: Node.js and websockets intro

Node.js

Created by Ryan Dahl

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

http://nodejs.org/

Montag, 26. April 2010

Page 4: Node.js and websockets intro

Evented?

Old (blocking) school:

<?php$content = file_get_contents("/some/huge/file");doThings($content); // Waiting, synchronotherThing();

Montag, 26. April 2010

Page 5: Node.js and websockets intro

Evented?

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

Montag, 26. April 2010

Page 6: Node.js and websockets intro

Benefits

Asynchronous programming

Event-loops instead of threads

Non-blocking

1 Thread (No context switching etc.)

Montag, 26. April 2010

Page 7: Node.js and websockets intro

CommonJS

Montag, 26. April 2010

Page 8: Node.js and websockets intro

CommonJS

A collection/library of standards

Modules

Binary

File system

and many more!

Montag, 26. April 2010

Page 9: Node.js and websockets intro

CommonJS Modules

There should be a function require

There should be a var called exports

Montag, 26. April 2010

Page 10: Node.js and websockets intro

// 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

Page 11: Node.js and websockets intro

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

Page 12: Node.js and websockets intro

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

Page 13: Node.js and websockets intro

WebSockets

Montag, 26. April 2010

Page 14: Node.js and websockets intro

WebSockets

A HTML5 standard

Allows bidirectional communications

Less overhead than HTTP/AJAX (less headers)

Cross domain

Montag, 26. April 2010

Page 15: Node.js and websockets intro

PollingMailmanClient Server

Montag, 26. April 2010

Page 16: Node.js and websockets intro

BidirectionalClient ServerTelephone

Montag, 26. April 2010

Page 17: Node.js and websockets intro

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

Page 18: Node.js and websockets intro

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

Page 19: Node.js and websockets intro

Demo!

Montag, 26. April 2010