87
WHERE NODE.JS MEETS IOS Enabling Node.JS Technology for Mobile Applications

Where Node.JS Meets iOS

Embed Size (px)

DESCRIPTION

Writing iOS apps in Javascript is not a new idea, anymore, at least since companies like Appcelerator (Titanium) built entire business models around corresponding frameworks. And yet, Apple manages to open up two exciting new possibilities during the WWDC 2013: The release of the JavaScriptCore Framework as a public API on iOS and OS X, as well as the announcement of an Objective-C to Javascript Bridge. I'd like to talk to you about my experiences with these new bridge-technologies, the new ways in which you can use them and finally present to you my own project; Node.app — a Node.js implementation for iOS.

Citation preview

Page 1: Where Node.JS Meets iOS

WHERE NODE.JS MEETS IOSEnabling Node.JS Technology for Mobile Applications

Page 2: Where Node.JS Meets iOS

HI!• Sam (@periping)

(github.com/srijs)

• CS Student at LUH

• Working at doctape (node.js & mobile)

• Javascript, Objective-C, plain C, Haskell

Page 3: Where Node.JS Meets iOS

WHERE NODE.JS MEETS IOS

Enabling Node.JS Technology for Mobile Applications

Page 4: Where Node.JS Meets iOS

JAVASCRIPT ON MOBILE.

Page 5: Where Node.JS Meets iOS

BAD REPUTATION

Page 6: Where Node.JS Meets iOS

BAD REPUTATION

“Slow”1.

Page 7: Where Node.JS Meets iOS

BAD REPUTATION

“Slow”1.Rough “Benchmarks” on iPhone 4S:

a) Fill an array (NSMutableArray vs. Array) with 1.000.000 strings

b) Call a function/method 1.000.000 times

Page 8: Where Node.JS Meets iOS

BAD REPUTATION

Fill an array with 1.000.000 strings (sec.)

Call a function/method 1.000.000 times (sec.)

Page 9: Where Node.JS Meets iOS

BAD REPUTATION

Fill an array with 1.000.000 strings (sec.)

Call a function/method 1.000.000 times (sec.)

Foundation

JavaScript

0 0.095 0.19 0.285 0.38

Page 10: Where Node.JS Meets iOS

BAD REPUTATION

Fill an array with 1.000.000 strings (sec.)

Call a function/method 1.000.000 times (sec.)

Foundation

JavaScript

0 0.095 0.19 0.285 0.38

Foundation

JavaScript

0 0.15 0.3 0.45 0.6

Page 11: Where Node.JS Meets iOS

BAD REPUTATION

NSMutableArray *a = [NSMutableArray new]; for (int i = 0; i < 1000000; i++) { a[i] = @“Hello World”; }

(function () { var a = []; for (var i = 0; i < 1000000; i++) { a[i] = ‘Hello World’; } })();

for (int i = 0; i < 1000000; i++) { [self exampleMethod]; }

- (void)exampleMethod { return; }

(function () { var fn = function () { return; }; for (var i = 0; i < 1000000; i++) { fn(); } })();

Page 12: Where Node.JS Meets iOS

BAD REPUTATION

Page 13: Where Node.JS Meets iOS

BAD REPUTATION

“Bloated”2.

Page 14: Where Node.JS Meets iOS

BAD REPUTATION

“Bloated”2.“Memory Hungry”

3.

Page 15: Where Node.JS Meets iOS

HOW TOJS / IOS

Page 16: Where Node.JS Meets iOS

HOW TO JS ON IOS

Web View w/ HTML/CSS/JS

Content

Page 17: Where Node.JS Meets iOS

HOW TO JS ON IOS

Web View w/ HTML/CSS/JS

ContentA. Slow start-up

Page 18: Where Node.JS Meets iOS

HOW TO JS ON IOS

Web View w/ HTML/CSS/JS

ContentA. Slow start-upB. Consumes lots of memory

Page 19: Where Node.JS Meets iOS

HOW TO JS ON IOS

Bundle own JS Engine

Page 20: Where Node.JS Meets iOS

HOW TO JS ON IOS

Bundle own JS Engine

A. Bloated Binary

Page 21: Where Node.JS Meets iOS

HOW TO JS ON IOS

Bundle own JS Engine

A. Bloated BinaryB. High memory footprint

Page 22: Where Node.JS Meets iOS

BAD REPUTATION

“Bloated”2.“Memory Hungry”

3.

Page 23: Where Node.JS Meets iOS

HOW TO JS ON IOS

JavaScriptCore Framework on iOS7

Low-overhead JS Context

Page 24: Where Node.JS Meets iOS

EXISTING SOLUTIONS.

Page 25: Where Node.JS Meets iOS

HTML UI Platform bridge

LANDSCAPE

Page 26: Where Node.JS Meets iOS

HTML UI Platform bridge

LANDSCAPE

Page 27: Where Node.JS Meets iOS

HTML UI Platform bridge

Touch

LANDSCAPE

Page 28: Where Node.JS Meets iOS

HTML UI Platform bridge

Titanium

Touch

LANDSCAPE

Page 29: Where Node.JS Meets iOS

HTML UI Platform bridge

PhoneGap

Titanium

Touch

LANDSCAPE

Page 30: Where Node.JS Meets iOS

Platform bridgeHTML UI

TECHNOLOGY

Page 31: Where Node.JS Meets iOS

Platform bridgeHTML UI

TECHNOLOGY

Page 32: Where Node.JS Meets iOS

Platform bridgeHTML UI

Proprietary APIs

TECHNOLOGY

Page 33: Where Node.JS Meets iOS

Platform bridgeHTML UI

Proprietary APIs

TECHNOLOGY

Page 34: Where Node.JS Meets iOS

CAN WE DO BETTER?

Page 35: Where Node.JS Meets iOS

TECHNOLOGY

?

Page 36: Where Node.JS Meets iOS

Frontend

TECHNOLOGY

?

Page 37: Where Node.JS Meets iOS

BackendFrontend

TECHNOLOGY

?

Page 38: Where Node.JS Meets iOS

Ti.Network.Socket.createTCP({ connected: function (e) {…} });

Y U SO PROPRIETARY?

Page 39: Where Node.JS Meets iOS

Ti.Network.Socket.createTCP({ connected: function (e) {…} });

net.createServer(function (s) {…

});

Y U SO PROPRIETARY?

Page 40: Where Node.JS Meets iOS

Ti.Stream.write(e.socket, Ti.createBuffer({ value: ‘Hello World' }), cb);

Y U SO PROPRIETARY?

Page 41: Where Node.JS Meets iOS

Ti.Stream.write(e.socket, Ti.createBuffer({ value: ‘Hello World' }), cb);

socket.write(new Buffer(‘Hello World’)

);

Y U SO PROPRIETARY?

Page 42: Where Node.JS Meets iOS

Platform bridgeHTML UI

TECHNOLOGY

Page 43: Where Node.JS Meets iOS

Platform bridgeHTML UI

TECHNOLOGY

Page 44: Where Node.JS Meets iOS

Platform bridgeHTML UI

TECHNOLOGY

Page 45: Where Node.JS Meets iOS

LET’S MAKE NODE.JS FOR

MOBILE!

Page 46: Where Node.JS Meets iOS

AKA “ARE YOU NUTS?!”

Page 47: Where Node.JS Meets iOS

PROOF-OF-CONCEPT

Page 48: Where Node.JS Meets iOS

PROOF-OF-CONCEPT

Page 49: Where Node.JS Meets iOS

>20.000 Visits !

>1.000 Subscribers !

>900 Stars on GitHub

PROOF-OF-CONCEPT

Page 50: Where Node.JS Meets iOS

“Can't wait to see where this goes.

Hack on my man, Hack on!”

>20.000 Visits !

>1.000 Subscribers !

>900 Stars on GitHub

PROOF-OF-CONCEPT

Page 51: Where Node.JS Meets iOS

LET’S ACTUALLY DO THIS!

Page 52: Where Node.JS Meets iOS

NODE.JS CODEBASE

Source: ohloh.net/p/node

Page 53: Where Node.JS Meets iOS

NODE.JS CODEBASE

~1.5M LOCSource: ohloh.net/p/node

Page 54: Where Node.JS Meets iOS

NODE.JS CODEBASE

OS

Node.js API

libuv (io)

v8c-ares (dns)http_parser zlib

openssl

native modules (js)

bindings (c++)

Page 55: Where Node.JS Meets iOS

NODE.JS CODEBASE

OS

Node.js API

libuv (io)

v8c-ares (dns)http_parser zlib

openssl

native modules (js)

bindings (c++)

Page 56: Where Node.JS Meets iOS

NODE.JS CODEBASE

OS

Node.js API

libuv (io)

v8c-ares (dns)http_parser zlib

openssl

native modules (js)

bindings (c++)

JavaScriptCore.framework

Page 57: Where Node.JS Meets iOS

NODE.JS CODEBASE

OS

Node.js API

libuv (io)

v8c-ares (dns)http_parser zlib

openssl

native modules (js)

bindings (c++)

JavaScriptCore.framework

bindings (Obj-C)

Page 58: Where Node.JS Meets iOS

bindings (c++)

~20.000 LOC

src$ wc -l *.cc 1180 cares_wrap.cc 196 fs_event_wrap.cc 136 handle_wrap.cc 3442 node.cc 668 node_buffer.cc 1066 node_constants.cc 631 node_contextify.cc 141 node_counters.cc 4179 node_crypto.cc 449 node_crypto_bio.cc 265 node_crypto_clienthello.cc 327 node_dtrace.cc 68 node_extensions.cc 1100 node_file.cc 603 node_http_parser.cc 58 node_javascript.cc 67 node_main.cc 305 node_os.cc 143 node_stat_watcher.cc 99 node_watchdog.cc 206 node_win32_etw_provider.cc 335 node_win32_perfctr_provider.cc 611 node_zlib.cc 293 pipe_wrap.cc 296 process_wrap.cc 117 signal_wrap.cc 499 smalloc.cc 604 stream_wrap.cc 751 string_bytes.cc 464 tcp_wrap.cc 160 timer_wrap.cc 724 tls_wrap.cc 183 tty_wrap.cc 442 udp_wrap.cc 65 uv.cc 20873 total

APPROACH

• Filesystem • Crypto • HTTP Parser • zlib • TCP • Timer • TLS • UDP

Page 59: Where Node.JS Meets iOS

bindings (Obj-C)

~2.000 LOC

APPROACH

• Filesystem • HTTP Parser • TCP • Timer • Buffer

Nodelike$ wc -l *.m 86 NLBinding.m 119 NLBindingBuffer.m 213 NLBindingConstants.m 309 NLBindingFilesystem.m 35 NLBindingSmalloc.m 34 NLBindingUv.m 162 NLCaresWrap.m 187 NLContext.m 364 NLHTTPParser.m 83 NLHandle.m 26 NLProcess.m 254 NLStream.m 197 NLTCP.m 70 NLTimer.m 33 NLUDP.m 2172 total

Page 60: Where Node.JS Meets iOS

BUT DOES IT WORK?

Page 61: Where Node.JS Meets iOS
Page 62: Where Node.JS Meets iOS

STOP! DEMOTIME!

Page 63: Where Node.JS Meets iOS
Page 64: Where Node.JS Meets iOS

https://itunes.apple.com/app/id793774475

Page 65: Where Node.JS Meets iOS

WHAT ALREADY WORKS…

Page 66: Where Node.JS Meets iOS

WHAT ALREADY WORKS…Assertion Testing

Path Punycode

Query Strings URL

Utilities

Reliable

Page 67: Where Node.JS Meets iOS

WHAT ALREADY WORKS…Assertion Testing

Path Punycode

Query Strings URL

Utilities

Reliable

Stream Events Buffer

Reliable

Page 68: Where Node.JS Meets iOS

WHAT ALREADY WORKS…Assertion Testing

Path Punycode

Query Strings URL

Utilities

Reliable

File System Timers

Reliable

Stream Events Buffer

Reliable

Page 69: Where Node.JS Meets iOS

WHAT ALREADY WORKS…Assertion Testing

Path Punycode

Query Strings URL

Utilities

Reliable

File System Timers

Reliable

DNSBasic

Stream Events Buffer

Reliable

Page 70: Where Node.JS Meets iOS

WHAT ALREADY WORKS…Assertion Testing

Path Punycode

Query Strings URL

Utilities

Reliable

File System Timers

Reliable

DNSBasic

Net, HTTPUnstable

Stream Events Buffer

Reliable

Page 71: Where Node.JS Meets iOS

WHAT’S THE USE…

Page 72: Where Node.JS Meets iOS

WHAT’S THE USE…

A. JavaScript Logic inside a native App

Page 73: Where Node.JS Meets iOS

WHAT’S THE USE…

A. JavaScript Logic inside a native App

B. Attach to a WebView (á la node-webkit)

Page 74: Where Node.JS Meets iOS

WHAT’S TO COME…

Page 75: Where Node.JS Meets iOS

WHAT’S TO COME…

• More Core Modules (Crypto, Zlib,…)

Page 76: Where Node.JS Meets iOS

WHAT’S TO COME…

• More Core Modules (Crypto, Zlib,…)• libuv GCD integration

Page 77: Where Node.JS Meets iOS

WHAT’S TO COME…

• More Core Modules (Crypto, Zlib,…)• libuv GCD integration• Testing, testing, testing…

Page 78: Where Node.JS Meets iOS

WHAT’S TO COME…

• More Core Modules (Crypto, Zlib,…)• libuv GCD integration• Testing, testing, testing…• Integration with Titanium, PhoneGap, others…

Page 79: Where Node.JS Meets iOS

PARTICIPATE

Page 80: Where Node.JS Meets iOS

PARTICIPATE

• Download the App from the AppStore

Page 81: Where Node.JS Meets iOS

PARTICIPATE

• Download the App from the AppStore• Test the modules that should work “reliably”

Page 82: Where Node.JS Meets iOS

PARTICIPATE

• Download the App from the AppStore• Test the modules that should work “reliably”• Subscribe to the Mailing List

Page 83: Where Node.JS Meets iOS

PARTICIPATE

• Download the App from the AppStore• Test the modules that should work “reliably”• Subscribe to the Mailing List• File GitHub Issues

Page 84: Where Node.JS Meets iOS

PARTICIPATE

• Download the App from the AppStore• Test the modules that should work “reliably”• Subscribe to the Mailing List• File GitHub Issues• Spread the word

Page 85: Where Node.JS Meets iOS

PARTICIPATE

• Download the App from the AppStore• Test the modules that should work “reliably”• Subscribe to the Mailing List• File GitHub Issues• Spread the word• Talk to me if you want to participate

Page 86: Where Node.JS Meets iOS

Q?

Page 87: Where Node.JS Meets iOS

Image Credits: http://bassetthounds.wordpress.com/category/basset-hound/ http://woofgallery.org/category/happy-dog-photos/

nodeapp.org github.com/node-app

@periping github.com/srijs

https://itunes.apple.com/app/id793774475