+ All Categories
Home > Technology > JS everywhere 2011

JS everywhere 2011

Date post: 10-May-2015
Category:
Upload: oleg-podsechin
View: 3,475 times
Download: 4 times
Share this document with a friend
Popular Tags:
68
The Future of Server Side JavaScript
Transcript
Page 1: JS everywhere 2011

The Future of Server Side JavaScript

Page 2: JS everywhere 2011

/me

#startups#akshell#ringojs

#moscowjs#dailyjs

Page 3: JS everywhere 2011
Page 4: JS everywhere 2011

Node.js

Ideal use cases

– Real time apps, infrastructure duct tape

Badly suited for

– CRUD, command line tools, CPU heavy loads

Easy to get started with

– Difficult to get into production

Page 5: JS everywhere 2011
Page 6: JS everywhere 2011
Page 7: JS everywhere 2011

Akshell NarwhalJS

RingoJS Wakanda

GPSEE

v8cgi

Page 8: JS everywhere 2011

ServerJS Fragmentation

Node.js CommonJS Wakanda RingoJS v8cgi Narwhal Common Node Flussperfd Akshell GPSEE0

2000

4000

6000

8000

10000

12000

Project Followers

Group Members

Fo

llow

ers

Page 9: JS everywhere 2011

Sync vs. Async

Page 10: JS everywhere 2011
Page 11: JS everywhere 2011

“async style is kicking sync style's back end”

Page 12: JS everywhere 2011
Page 13: JS everywhere 2011

sync is “on top of” async – a higher level of abstraction

Page 14: JS everywhere 2011

Sync vs. Async

function add(callback) { http.get(url1, function(response1) { var part1 = response1.data; http.get(url2, function(response2) { var part2 = response2.data; callback(part1 + part2); } }}

Page 15: JS everywhere 2011

Sync vs. Async

http.get(url1).data + http.get(url2).data

Page 16: JS everywhere 2011

Interoperability

Pure JavaScript modules run anywhere Templating, parsing, formatting, encoding

Anything doing I/O exposes sync or async API Defines the interface exposed by higher level

packages

Page 17: JS everywhere 2011
Page 18: JS everywhere 2011
Page 19: JS everywhere 2011

CommonJS /1

Modules/1.1Packages/1.0

AssertConsoleSystem/1.0

Page 20: JS everywhere 2011

CommonJS /2

Binary/B

IO/AJSGI 0.3

Filesystem/A

Page 21: JS everywhere 2011

CommonJS /3

HTTP Client /ASockets/A

Subprocess

Page 22: JS everywhere 2011

common-node

Implements synchronous CommonJS proposals using node-fibers

Traceur to support latest language features

Bridges the gap between platforms, sync and async

Page 23: JS everywhere 2011

What it's good for?

Page 24: JS everywhere 2011

everything!

Page 25: JS everywhere 2011

What's it actually good for?

Business logic

– Lots of state, fine grained error handling CRUD

– Java, Rails, PHP, Python Command line tools Cross platform portability

Page 26: JS everywhere 2011

On Threads & Fibers

“Threads suck”

- Brendan Eich, creator of JavaScript

“Fibers introduce interleaving hazards — Any function call can cause a yield and then your closure invariants *may be* broken.”

- Kris Kowal, creator of CommonJS/Modules

Page 27: JS everywhere 2011

Concurrency in JavaScript

“You shouldn’t think that event-based concurrency eliminates synchronization, or shared memory, or anything other than preemption”

- Sam Tobin-Hochstadt, member of the Ecma TC39 committee on JavaScript

Page 28: JS everywhere 2011

fibers /1

Co routine implementation using libcoro

Co-operative multitasking

Implicit synchronization

Page 29: JS everywhere 2011

fibers /2

Not a fork or a hack of Node

No wrapper script required

V8 Context and 64KB stack per fiber

Will run on Windows

Page 30: JS everywhere 2011

Node.js

process[ closure

closure

t →

Page 31: JS everywhere 2011

RingoJS (0.8)

process thread stack

processthread stack

t →

Page 32: JS everywhere 2011

Common Node (fibers)

processfiber stack

fiber stack[

t →

Page 33: JS everywhere 2011

Node.js Common Node RingoJS

Process Count Single Single Multiple

State Closure Fiber Stack Thread Stack

Multitasking User (co-op) Library (co-op) OS (pre-empt)

Memory Usage Low Low High

“Jitter” High High Low

Page 34: JS everywhere 2011

Internals - sleep

exports.sleep = function(milliseconds) {

var fiber = Fiber.current;

setTimeout(function() {

fiber.run();

}, milliseconds);

yield();

};

Page 35: JS everywhere 2011

Internals – HttpClient /1

var req = http.request(options, function(r) {

fiber.run(r);

});

req.on('error', function(error) {

fiber.run(error);

});

this.guts.body.forEach(function(block) {

req.write(block.buffer || block);

});

req.end();

Page 36: JS everywhere 2011

Internals – HttpClient /2

var result = yield();

if(result instanceof Error)

throw new Error(result.message);

return {

status: result.statusCode,

headers: result.headers,

body: new Stream(result)

};

Page 37: JS everywhere 2011

Internals - IO

// on 'data', 'end', 'error'

// pause when draining

var listeners = attach(this.stream);

var data = yield();

detach(this.stream, listeners);

Page 38: JS everywhere 2011

Examples

Page 39: JS everywhere 2011

JSGI

exports.app = function(request) {

return {

status: 200,

headers: {},

body: ['Hello World!']

// openRaw(module.filename)

};

};

Page 40: JS everywhere 2011

Spawn & Sleep

exports.app = function(request) {

spawn(function() {

sleep(10000);

console.log('Hello Server!');

});

return {

status: 200,

headers: {},

body: ['Hello Client!']

};

};

Page 41: JS everywhere 2011

HTTP Proxy

var HttpClient = require('httpclient').HttpClient;

exports.app = function(req) {

req.url = 'http://nodejs.org';

return new HttpClient(req).finish();

};

Page 42: JS everywhere 2011

Twitter Streaming /1

var stream = new TextStream( new HttpClient({

method: 'POST',

url: '...',

headers: {}

body: ['track='+system.args[4]],

timeout: 10000

}).finish().body);

Page 43: JS everywhere 2011

Twitter Streaming /2

var line;

while(true) {

line = stream.readLine();

if(!line.length) break;

if(line.length > 1) {

var message = JSON.parse(line);

console.log(message.text);

}

}

Page 44: JS everywhere 2011

Benchmarksab -n 50000 -c 50

Page 45: JS everywhere 2011

hello-world

exports.app = function() {

return {

status: 200,headers: {

'Content-Type': 'text/plain'},body: ['Hello World!\n']

};

};

Page 46: JS everywhere 2011
Page 47: JS everywhere 2011

string-alloc

exports.app = function(request) {

for( var i = 1; i <= 50; i++)

b.decodeToString("ascii");return {

status: 200,headers: {},body: [b]

};

};

Page 48: JS everywhere 2011
Page 49: JS everywhere 2011

parse-json

exports.app = function(request) {

JSON.parse(json);

return {

status: 200,headers: {},body: [json]

};

};

Page 50: JS everywhere 2011
Page 51: JS everywhere 2011

static-file

exports.app = function() {

return {

status: 200,

headers: {},

body: openRaw('../README.md')

};

};

Page 52: JS everywhere 2011
Page 53: JS everywhere 2011

set-timeout

exports.app = function() {

sleep(100);

return {

status: 200,

headers: {},

body: []

};

};

Page 54: JS everywhere 2011
Page 55: JS everywhere 2011

Throughput

hello-world string-alloc parse-json static-file set-timeout0

1000

2000

3000

4000

5000

6000

Node.js

Common Node

RingoJS

Page 56: JS everywhere 2011

Contributing

Google “common node”

github.com/olegp/common-node/

npm -g install common-node

Page 57: JS everywhere 2011

SyncJS Fragmentation

Wakanda RingoJS v8cgi Narwhal Common Node Flussperfd Akshell GPSEE0

50

100

150

200

250

300

350

400

Project Followers

Group Members

Fo

llow

ers

Page 58: JS everywhere 2011

Toolkits vs. Frameworks

Page 59: JS everywhere 2011
Page 60: JS everywhere 2011
Page 61: JS everywhere 2011
Page 62: JS everywhere 2011
Page 63: JS everywhere 2011
Page 64: JS everywhere 2011

Next Steps

Stick backportDatabase access–selectjs.com

Higher level packages (wiki)

Page 65: JS everywhere 2011

common-utils

base64: encode, decode hash: sha1 etc. url: parse, format, resolve string: format, trim etc. date: format, add, before, after etc. array: contains etc. object: clone, merge etc.

Page 66: JS everywhere 2011

One more thing ...

Third party services & APIs olegp/rest-wrapper thelockerproject.org

Browser based IDEs (Cloud9, Akshell) Social hosting

PINF, automatic redeployment, continuous integration etc.

Page 67: JS everywhere 2011

Summary

sync and async will co-exist toolkits instead of frameworks implementation driven de-facto

standards we are just getting started!

Page 68: JS everywhere 2011

Thank you!

@olegpodsechin


Recommended