Node.js #digpen presentation

71
Node.js: JavaScript from Client to Server and Beyond! Robert McCarthy CEO Gary Ratclliffe Development Manager

description

JavaScript from Client to Server and Beyond!

Transcript of Node.js #digpen presentation

Page 1: Node.js #digpen presentation

Node.js:

JavaScript from Client to Server and Beyond!

Robert McCarthy –CEO

Gary Ratclliffe – Development Manager

Page 2: Node.js #digpen presentation

2

Agenda

1. The best new technology in the world

2. The exciting future

3. The real world

4. If still alive

Goto 1

Page 3: Node.js #digpen presentation

Introduction to GOSS

Robert McCarthy – GOSS CEO

Gary Ratclliffe – Technical

Page 4: Node.js #digpen presentation

Who we work with

4

Page 5: Node.js #digpen presentation

5

Server-side Javascript

Page 6: Node.js #digpen presentation

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

Page 7: Node.js #digpen presentation

7

Why?

Page 8: Node.js #digpen presentation
Page 9: Node.js #digpen presentation

9

The Problem?

Page 10: Node.js #digpen presentation

10

Form validation for websites

User needs simple validation

Page 11: Node.js #digpen presentation

11

On the browser

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

Page 12: Node.js #digpen presentation

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)

Page 13: Node.js #digpen presentation

13

4 different implementations!!

More work, more maintenance, more risk

Page 14: Node.js #digpen presentation

14

The Solution?

Server Side javascript = One language

Less work, less maintenance, less risk

Page 15: Node.js #digpen presentation

15

The Implementation

Java

Site

.NET

Site

CF

Site

Browser

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

Mozilla Rhino

MS Jscript

Page 16: Node.js #digpen presentation

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)

Page 17: Node.js #digpen presentation

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

Page 18: Node.js #digpen presentation

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

Page 19: Node.js #digpen presentation

19

Snag

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

JScript was problem

Page 20: Node.js #digpen presentation

20

V8 Arrives!

Created by Google for Chrome

Also designed to be used in other apps

Page 21: Node.js #digpen presentation

21

V8 is FAST!!

Clever optimisations and compiling to native code.

Not quite as dramatic now as when it first appeared

Page 22: Node.js #digpen presentation

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

Page 23: Node.js #digpen presentation

23

V8.net wrapper

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

Real JavaScript

Much faster than Jscript!!

Page 24: Node.js #digpen presentation

24

V8 is Single Threaded!!

A problem for traditional server-side scalability

How do you handle multiple concurrent requests?

Page 25: Node.js #digpen presentation

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

Page 26: Node.js #digpen presentation

26

Along came Node.JS

Page 27: Node.js #digpen presentation

27

Along came Node.JS

Page 28: Node.js #digpen presentation

28

Non blocking vs Threads

Threads use a lot of memory and are limited

Use asynchronous IO and an event loop like NGINX

Page 29: Node.js #digpen presentation

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

Page 30: Node.js #digpen presentation

30

You’ve gotta love it

Page 31: Node.js #digpen presentation

Node.JS

Page 32: Node.js #digpen presentation

32

What can Node.js do?

Simple http hello world

Page 33: Node.js #digpen presentation

33

Simple web server

Continuations break processing into steps

Function called on each request

Function called when file contents available

Page 34: Node.js #digpen presentation

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

Page 35: Node.js #digpen presentation

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!

Page 36: Node.js #digpen presentation

1000’s of concurrent requests

All server-side JavaScript

Page 37: Node.js #digpen presentation

Writing continuations for everything is hard

Sooner or later something has to do something

Page 38: Node.js #digpen presentation

38

Writing continuations for everything is hard

Page 39: Node.js #digpen presentation

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

Page 40: Node.js #digpen presentation

40

Our Solution

Lets call it pragmatic

Page 41: Node.js #digpen presentation

41

IcedCoffeeScript?

Not a drink but derived from coffeesript

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

Page 42: Node.js #digpen presentation

42

IcedCoffeeScript?

What happen to the 1 language

It makes you think different.

Page 43: Node.js #digpen presentation

43

await and defer used to

express callbacks

Cleaner code layout but

you do have to learn

CoffeeScript

Extra build step to

generate JavaScript

Page 44: Node.js #digpen presentation

44

And that other issue?

Page 45: Node.js #digpen presentation

45

Sooner or later something has to

do something

For many things this is not a problem

eg

Database modules written to be asynchronous

Page 46: Node.js #digpen presentation

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?

Page 47: Node.js #digpen presentation

47

V8 is single threaded!

A badly written extension and Bye Bye server…

Page 48: Node.js #digpen presentation

48

Use Node.JS for it’s strengths

Page 49: Node.js #digpen presentation

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

Page 50: Node.js #digpen presentation

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

Page 51: Node.js #digpen presentation

51

Where Next?

Does the industry need Node.JS?

Other web servers can/will achieve similar performance

So its really all about the language?

Page 52: Node.js #digpen presentation

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?

Page 53: Node.js #digpen presentation

53

So will it survive?

Page 54: Node.js #digpen presentation

54

Today most sites..

Page 55: Node.js #digpen presentation

55

Multiple Dev Languages

Page 56: Node.js #digpen presentation

56

Client is a dumb browser

Page 57: Node.js #digpen presentation

57

Clients are isolated from one another

Page 58: Node.js #digpen presentation

58

Anything more interactive tends to be bespoke

Page 59: Node.js #digpen presentation
Page 60: Node.js #digpen presentation

60

Imagine

Page 61: Node.js #digpen presentation

61

Imagine

One server-side infrastructure

Page 62: Node.js #digpen presentation

62

Imagine

One Language

Page 63: Node.js #digpen presentation

63

Imagine

One Data Format: JSON?

Page 64: Node.js #digpen presentation

64

Imagine

Transfer Data not UI

Page 65: Node.js #digpen presentation

65

Imagine

Collaborate & Distribute

Page 66: Node.js #digpen presentation

66

Page 67: Node.js #digpen presentation

67

So is it just a dream?

Page 68: Node.js #digpen presentation

68

Page 69: Node.js #digpen presentation

69

But that’s for the next talk?

Page 70: Node.js #digpen presentation

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…

Page 71: Node.js #digpen presentation

Any questions?

Thank you!!

PS. We are hiring!