Node.js #digpen presentation

Post on 24-Dec-2014

622 views 3 download

description

JavaScript from Client to Server and Beyond!

Transcript of Node.js #digpen presentation

Node.js:

JavaScript from Client to Server and Beyond!

Robert McCarthy –CEO

Gary Ratclliffe – Development Manager

2

Agenda

1. The best new technology in the world

2. The exciting future

3. The real world

4. If still alive

Goto 1

Introduction to GOSS

Robert McCarthy – GOSS CEO

Gary Ratclliffe – Technical

Who we work with

4

5

Server-side Javascript

6

How many of you use JavaScript?

Put your hands UP if YES

Is the majority of your JavaScript jQuery?

Put your hands DOWN if YES

Are you using any server-side JavaScript?

Put yours hands DOWN if NO

7

Why?

9

The Problem?

10

Form validation for websites

User needs simple validation

11

On the browser

/^[A-D][0-9]{4}$/i.test(v)

12

On the server

CF: ReFindNoCase("^[A-D][0-9]{4}$", v) GT 0

Java: Pattern.matches("(?i)^[A-D][0-9]{4}$", v)

.NET Regex.Match(v, "^[A-D][0-9]{4}$", RegexOptions.IgnoreCase)

13

4 different implementations!!

More work, more maintenance, more risk

14

The Solution?

Server Side javascript = One language

Less work, less maintenance, less risk

15

The Implementation

Java

Site

.NET

Site

CF

Site

Browser

/^[A-D][0-9]{4}$/i.test(v)

Mozilla Rhino

MS Jscript

16

What Next?

Like the idea of common language on all platforms

(But Clients still expect .NET sites to be .NET like & Java sites to be Java like)

17

Entire Forms engine?

Complex Benefits from implementing once

Needs to be extensible

There is always a new requirements Clients could create their own extensions

18

So we did it!

Forms generated using a simple template* containing JavaScript

<%

if ( Props["FIELDPROPERTIES"]["MAXLENGTH"] !== "") {

maxLength="maxlength='" + Props["FIELDPROPERTIES"]["MAXLENGTH"] + "'";

}

%>

<label for='<%=Props["FIELDID"]%>'><%=Processor.htmlEsc(Props["FIELDPROPERTIES"]["LABEL"])%></label>

<input type="text" name="<%=Props["FIELDID"]%>" id="<%=Props["FIELDID"]%>" <%=maxLength%>/>

19

Snag

We really needed a good, fast JavaScript engine on all platforms.

JScript was problem

20

V8 Arrives!

Created by Google for Chrome

Also designed to be used in other apps

21

V8 is FAST!!

Clever optimisations and compiling to native code.

Not quite as dramatic now as when it first appeared

22

V8 is Single Threaded!!

Only one thread can execute JavaScript at a time

V8 Locking can help synchronize multiple threads

One of reasons it’s fast

Multi-threading can get complex…and slow

23

V8.net wrapper

Replaced JScript with JavaScript .NET from Noesis Proved reliable in production deployment

Real JavaScript

Much faster than Jscript!!

24

V8 is Single Threaded!!

A problem for traditional server-side scalability

How do you handle multiple concurrent requests?

25

V8.net wrapper

Limited by single threading Resorted to a global lock around execution

But an evolutionary dead-end while V8 is single threaded

26

Along came Node.JS

27

Along came Node.JS

28

Non blocking vs Threads

Threads use a lot of memory and are limited

Use asynchronous IO and an event loop like NGINX

29

Weakness now a strength

Ryan Dahl, creator of Node.JS used these concepts in Node.JS

The single threaded nature of V8 was no longer a problem

30

You’ve gotta love it

Node.JS

32

What can Node.js do?

Simple http hello world

33

Simple web server

Continuations break processing into steps

Function called on each request

Function called when file contents available

34

Node.JS

Lots of additional modules

Really learn JavaScript the language not just jQuery

Google ‘Douglas Crockford’

Look at source code of real projects

Combine it with MongoDB for end-to-end JSON

35

Node.JS

Think carefully if your application needs it

Highly interactive?

Loads of AJAX?

Very high number of requests?

Not just because you can!

1000’s of concurrent requests

All server-side JavaScript

Writing continuations for everything is hard

Sooner or later something has to do something

38

Writing continuations for everything is hard

39

Real world code is hard

How many developers

really understand it?

Many can tweak, but

starting from scratch…

Debugging can be difficult

Node-inspector worth a

look

40

Our Solution

Lets call it pragmatic

41

IcedCoffeeScript?

Not a drink but derived from coffeesript

CoffeeScript = Cool, elegant syntax, JavaScript without the Bad Bits

42

IcedCoffeeScript?

What happen to the 1 language

It makes you think different.

43

await and defer used to

express callbacks

Cleaner code layout but

you do have to learn

CoffeeScript

Extra build step to

generate JavaScript

44

And that other issue?

45

Sooner or later something has to

do something

For many things this is not a problem

eg

Database modules written to be asynchronous

46

This example is computationally

intensive. It results in high CPU

use on any system.

On Node.JS it would only

process one request at a time.

n=40 takes about 5s

3 concurrent requests:

1 client will wait 15s

Non blocking?

47

V8 is single threaded!

A badly written extension and Bye Bye server…

48

Use Node.JS for it’s strengths

49

Multiple workers processes

JavaScript Worker

TeaJS

JavaScript Worker

TeaJS

JavaScript Worker

TeaJS

Java Worker

Java VM

Java Worker

Java VM

Java Worker

Java VM

Node.JS Worker

Node.JS

Node.JS Worker

Node.JS

Node.JS Worker

Node.JS

Worker Manager

Node.JS

Management Security

Requests

50

Best of all worlds

Requests delegated to a suitable worker or queued if worker busy

A CGI style wrapper round the V8 engine allowing use of traditional linear JavaScript. It’s a good match for our approach to forms rendering

51

Where Next?

Does the industry need Node.JS?

Other web servers can/will achieve similar performance

So its really all about the language?

52

Is 1 Language an ideal?

Same developers for UI and Backend?

Common in start-ups

Less so in the ‘enterprise’

Jack of all trades master of none?

53

So will it survive?

54

Today most sites..

55

Multiple Dev Languages

56

Client is a dumb browser

57

Clients are isolated from one another

58

Anything more interactive tends to be bespoke

60

Imagine

61

Imagine

One server-side infrastructure

62

Imagine

One Language

63

Imagine

One Data Format: JSON?

64

Imagine

Transfer Data not UI

65

Imagine

Collaborate & Distribute

66

67

So is it just a dream?

68

69

But that’s for the next talk?

70

Final Thoughts….

The web is changing at pace.

There will be lots of shiny toys

They might not lead to nirvana

but they will often give you a glimpse of a very exciting future!!

Stay Excited…

Any questions?

Thank you!!

PS. We are hiring!