Server side scripting smack down - Node.js vs PHP

24,631 views

Published on

Talk given to the audience at to the PHP London User Group June 2011

Rather than a comparison of the two languages this is actually an introduction to Node.js intended to be from the perspective of someone who already knows quite a bit about PHP, covering and comparing common concepts between the two and explaining differences in approaches.

The talk was given in a pub, after several drinks, do please forgive my lax presentation style.

Published in: Technology, Self Improvement
3 Comments
7 Likes
Statistics
Notes
  • @keepitdk Node.js is SERVER-side javascript which your browser settings can't affect .
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • if javascript is everywhere, what happens when someones browser's javascript is turned off? any suggestions for failovers?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • What a waste of time looking at these slides! I am not sure what the aim of the presentation was? By the way, I hope you got permission for all the images that you included in the slides!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
24,631
On SlideShare
0
From Embeds
0
Number of Embeds
259
Actions
Shares
0
Downloads
0
Comments
3
Likes
7
Embeds 0
No embeds

No notes for slide
  • Hello, and welcome to this Scripting Language Smackdown at PHP London 2011. We have a hell of a contest here tonight. INTRODUCING:\n
  • de facto scripting language since 1997,\npowering in excess of 20 million domains \nthe brainchild of Rasmus Lerdof: \n
  • \n
  • Just two years old and already demolishing the competition\nHeralded as the best thing since both sliced bread and PHP,\nCreated by Ryan Dahl:\n
  • \n
  • Okay - i can't do a whole talk like that...\nI'm going to talk to you today about PHP vs Node.JS, give a brief introduction to Node, comparing its strengths and weaknesses to PHPs strengths and weaknesses, and also try and give you some ideas of where and why you might want to use node in anger. I've only got 20 minutes so we'll go pretty quick. Lets take a look at the competitors:\n
  • I am not going to tell you a whole bunch of stuff about PHP, but I'm going to cover some key things you need to have in mind when weighing it up here:\n\n
  • mod_php is one of the main reasons PHP is so successful. What is it? Its the apache module that just plugs in and makes PHP work beautifully with the worlds most popular web server. What makes it great?\n\n
  • Its the killer move. It means each execution of a PHP script is independant of every other execution of that script. That means you dont have to worry about memory leaks, or garbage collection most of the time, not all of the time, but most of the time.\n
  • it is procedural - yes it does OO but what I mean that you wait for the of code above to finish execution before you run. Thats a pretty important point right there to remember. \n\nIn php you wait for the line above you to finish execution before you run. Its so obvious its almost insane to mention it right? Wrong. I'll show you why in a bit.\n\n
  • PHP is massive - tons of extensions, code libraries, frameworks,\nhuge install base/user base/documentation. \n\nruns some of the biggest sites on the interweb\n\nIts continued success is due, in part, to its past success.\n\n
  • So thats PHP. But this is a fight, so we need an avatar to represent PHP in this fight. If php was a pro wrestler this is who it’d be.\n
  • The hulkster. Its old, its classic, its an all rounder, everyone loves it (unless they hate it), its flamboyant while still being pragmatic.\n
  • On to node. \nIts _already_ cheating because its not a scripting language at all. its...\n
  • \n
  • What is that supposed to mean. As a catch phrase, its not even catchy or clever. Lets break it down and work through it. \n\n\n
  • I’m going to go backwards. Does everyone know what javascript is?\nAny one not know what javascript it?\n\nIts server-side javascript. Which if you’re from 2001, sounds like a horrible horrible idea. But javascript has changed since then, well it hasn’t changed, but the implementations have, and how we’re using it has. \n\nNow its the most widely used interpreted programming language.\n
  • V8, V8 javascript. Thats not version 8, its the javascript engine that runs inside Google Chrome. It is probably the reason you use chrome, if you use chrome. becuase it makes everything seem quicker. In googles own benchmarks, it outperforms other browsers by orders of magnitude. \n\nThats sort of because google cheats a bit, and the tests are heavily biased on recursive calls, which V8 is really good at. But that turns out to be good news for node and you’ll see why soon.\n
  • I’m going to hope that you all know that IO means input/output. \n
  • so i’ll explain what evented IO means\n
  • Imagine a webserver. So your client comes and asks for a page, you go get it, give it to them, and wait for the next request. \n
  • If another request comes in before you finish serving the first guy... they have to wait. You're waiting for the disk, or the database, or some API, or some other shit. \n\nUntil everything gets backed up waiting for the webserver to handle the requests in front\n
  • So you run multiple threads, so you can handle lots of concurrent requests. Behind the scenes you still have to worry about file locking and all that shit, but people are not waiting as long for their stuff. Maybe you're making a few duplicate disk reads, or db queries, but you can fix that with some caching.\n
  • Until you cant run enough threads anymore. So what do you do now? You need to take a different approach. \n
  • Instead of handling each request with a thread, you just accept the request, and process it in an 'event loop'. Here the thread loops through the requests and when it has to do some IO, it does it asyncronously.\n
  • It fires off a request for a resource, but doesn't wait for the response before processing the next task it has. When the resource has returned, the server is notified by an event, and calls the code it was told about when the request went out - the callback\n\nnearly EVERYTHING in node is event driven - you just chuck an event, and the callback will happen sometime in the future, and you don't really care when. - the server continues without blocking on that request. Doing this in PHP is like pulling teeth\n\n
  • Lets get this fight started... round one\n
  • mod_php vs what?\n
  • In node you can’t just plug it into an existing webserver and everything just works out of the box like you can in PHP. You have to write your own. At its most basic that looks like this. Obviously this gives you plenty of opportunity\n
  • to balls it all up yourself. Luckily node has a way round this. It has a pretty awesome package manager called npm and the community is pretty good too, so there are lots of modules available.\n
  • This is a list of some of the modules that operate as webservers. Some are micro frameworks in thier own right, some will serve static content pretty quick. But what i’m trying to say is that you dont _have_ to write your own. You just can if you want.\n\nCompare this with PHP though.\n
  • PHP runs on everything\n
  • \n
  • \n
  • \n
  • \n
  • this is all because of CGI - implemented by webservers since the dawn of time, or at least since i was in short trousers. \n
  • Node\n
  • Doesn’t even run on windows at the moment. But i’ll leave that up to you to decide whether thats a good thing or not\n\nSo how is our champ doing in the ring?\n
  • Here he is. He has hardly started. He’s not even finished taking off his vest. What a guy.\n
  • On to round two\n
  • Performance. Which of these is going to give you more bang for your buck?\n\n\n
  • Remember node is evented, so i guess that means it should be faster at everything right?\n
  • Who cares? I mean it. It doesn’t matter.\n\nbench mark it yourself for specific applications. \n\nYour appriasal process for using any technology in a commercial setting should not be based upon the benchmark comparison conducted by some blogger in turkmenistahn\n
  • neither should it be based on reports of how fast something it is by some guy in a pub (ie me).\n \n
  • truth is, node is probably faster for a whole bunch of applications, because its even more webscale than PHP. But really - benchmark it yourself.\n
  • Recently I saw some blog posts noting that PHP was faster, or used less memory. Its all down to the application.\n\nEnd of round 2, whats our champion up to?\n
  • Holy shit, he’s throwing that other dude around. He’s powering through this. He’s taken a couple of knocks but he’s started strong.\n
  • Round three... whats this about?\n
  • Oooh code. I love code. Its the enjoyment of bending something to my will.\n
  • Not going to talk a lot about PHP code, I hope you all know what it looks like, how it works. Quickly going to run through a PHP 5.3 feature\n\n
  • A closure is an anonymous function, which can be used as either a variable, or an argument to another function\n
  • OMG its a code slide. Dont worry not too many of these.\n\nHere is a closure in PHP. Its a function being assigned to a variable. Thats neat.\n
  • BANG - we’re calling that variable as though it was a function (which it is). Thats some crazy stuff. Look at it go. Whats next...\n
  • Oh yeah. We can pass that variable around as an argument. Lets array_walk with that guy.\nHe’s makin sandwiches like pret a manger.\n\nThats great and all, but its not really that useful. the variable isn’t really that different to a namespaced function call. Lets beef up the anonimity.\n
  • Now we’re just throwing that closure straight into array_walk. its just getting thrown around and disposed of.\n\n\n
  • If any of you have used much javascript, esp jQuery this should look familiar. You do this a lot in JavaScript.\n
  • But its here that node really steps up a gear though. \n
  • If you get your server side in javascript\n
  • and you already have your client side in javascript\n
  • and all the communication between them is all in javascript object notation...\n\nThen your complete stack is in JavaScript. Couple that with storage in a noSQL database that stores objects as json like mongodb.\n\n\n
  • If you’re using javascript everywhere you can invest in your client side app. You can use stuff like backbone.js onthe client and server side, and share your models\n
  • there are even logic-less templating systems in javascript. All of a sudden you only need to be familiar with one language.\n\nI’m not necessarily advocating that, but I do think its pretty cool.\n\nThats round 3 - ding ding. How is our champ doing?\n
  • Oh dear... he’s looking worn out now, he’s been going a while, and this sort of thing takes its toll on old men. Lets move on... i think he’s starting to sway\n
  • \n
  • Okay. No matter what app you write, at some point you’re going to have to persist some data somewhere, and your scripting language/framework/whatever needs to support that.\n
  • As I’ve already mentioned, there are some noSQL dbs that store everything in json, some of them have json apis.\n\nBecause of the nature of these things, the latest craze of node.js goes hand in hand with the craze of noSQL. there are a multitude of modules that support talking to these databases\n
  • and there is plenty of support for more traditional dbs too\n\nThe key thing is - they all work asyncronously. You fire off a query and dont wait for it to return, node just gets on with doing somehting else instead. In PHP - you ask for something and you better be prepared to wait for it to get back to you before you do anything else.\n
  • but PHP has PDO\na tidy (not perfect) abstraction - nothing like that in Node.js - its too fragmented at the moment\n
  • it has native drivers for mysqlnd.\n
  • it has loads of libraries and even some extensions for using noSQL dbs and just about every sort of relational db too\n\nding ding. Now lets see whats going on in the ring\n
  • Its not looking good is it. things are looking bad.\n
  • \n
  • Frameworks. PHP should have this in the bag right? I mean like how many frameworks are there available for php\n
  • Some of those are even pretty good. \n\nSo possibly, but i want to talk quickly about something node has which is pretty clever.\n
  • Express is a microframework inspired by Sinatra in Ruby.\nIt only really provides routing and templating, but has hooks for a lot of other bits. It is the easiest way to get started building node.js web apps.\n
  • This is the last code slide.\n\nIts the basic example from the express js site - its just wires a route to a function, a closure. What you pass in instead of that hello world can do whatever you like.\n\nIts so neat infact that the PHP world is starting to copy it.\n
  • Check out Silex. Its a PHP microframework by sensiolabs, the company behind Symfony, and it makes use of php 5.3's closures to provide the same sort of routing in PHP.\n\nSure its just routing, and routing is fairly easy, but doing things this way is unbelivably light weight, and its incredibly quick to get started building stuff this way.\n\n
  • appserver in php is a whole nother talk but its really pretty cool\n\nit breaks shared nothing completely, preconnecting to network resources like DBs, preloading configs/sharing resources and so forth. making response times really quick and letting you tie in node apps to your existing PHP back end.\n\nNow lets see how the Hulkster is faring...\n\n
  • HES ON HIS FEET\nTHE CROUD ARE GOING WILD.\n\nEmpowered by the croud he’s back up - ready to fight... Hulking up!\n
  • \n
  • Whats the bottom line.\n
  • PHP has still got it. We continue to keep up with the competition. There’s some innovation in there too.\n
  • node has its uses\n\n
  • ideal for bespoke daemons - you need a custom smtp something or other, or an image webservice or something\n\n\n
  • Use it for just about anything with websockets: sockets.io makes it unbelievably easy to do. Live chat or something like that\n\nWho won the fight? who knows. it could definatly go either way\n\n
  • Maybe node and PHP will end up getting on really well and making beautiful music together. \n
  • I’ve been marc gear\ndon’t follow me on twitter.\n\nI work at Moo.com, we print cool stuff like this.\n\nWe’re hiring developers, ping me your cv at the address on the screen.\n
  • Server side scripting smack down - Node.js vs PHP

    1. 1. SERVER SIDE SCRIPTINGMarc Gear PHP London June 2011
    2. 2. CHAMPION
    3. 3. CHAMPION
    4. 4. CONTENDER
    5. 5. CONTENDER
    6. 6. PHP vs node.js• super brief intro to node.js• strengths & weaknesses• where & why to use node.js
    7. 7. mod_php
    8. 8. shared nothing
    9. 9. procedural
    10. 10. massive
    11. 11. “Evented I/O for V8 JavaScript”
    12. 12. WTF?
    13. 13. “Evented I/O for V8 JavaScript”
    14. 14. “Evented I/O for V8 JavaScript”
    15. 15. “Evented I/O for V8 JavaScript”
    16. 16. “Evented I/O for V8 JavaScript”
    17. 17. Clients Webserver
    18. 18. Clients Webserver
    19. 19. Clients Webserver
    20. 20. Clients Webserver
    21. 21. Clients Webserver
    22. 22. callbacks
    23. 23. Round 1
    24. 24. Round 1mod_php vs ...
    25. 25. var http = require(http);http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);}).listen(1337, "127.0.0.1");console.log(Server running at http://127.0.0.1:1337/);
    26. 26. Lightnode beeline PicardNitrode biggie-router simplexantinode choreographer Pipe-layernode-paperboy clutch franknode-static connect webservice.jsoui dispatch zappastatic escort archetypestatic-resource journey brokeNi node-router chain(fab) node.routes.js coltrainekaph nozzle djangodeNerve sherpa drtyPrick rowan Expressrestmvc.js vroom Grasshopper
    27. 27. everywhere
    28. 28. apache
    29. 29. lightty
    30. 30. ngnix
    31. 31. IIS
    32. 32. cgi
    33. 33. Round 2
    34. 34. Round 2performance
    35. 35. evented
    36. 36. who cares?
    37. 37. me
    38. 38. webscale
    39. 39. PHP faster?
    40. 40. Round 3
    41. 41. Round 3 code
    42. 42. PHP
    43. 43. closures
    44. 44. $bread = rye;$sandwich = function($filling) use ($bread) { echo $bread, $filling, $bread;}
    45. 45. $bread = rye;$sandwich = function($filling) use ($bread) { echo $bread, $filling, $bread;}$sandwich(pastrami);$sandwich(egg & cress);
    46. 46. $bread = rye;$sandwich = function($filling) use ($bread) { echo $bread, $filling, $bread;}$fillings = array(pastrami, egg & cress);array_walk($fillings, $sandwich);
    47. 47. $bread = rye;$fillings = array(pastrami, egg & cress);array_walk($fillings, function($filling) use ($bread) { echo $bread, $filling, $bread;});
    48. 48. JavaScript
    49. 49. BrowserBrowser Server Webserver
    50. 50. BrowserBrowser Server Webserver JavaScript
    51. 51. BrowserBrowser Server Webserver JavaScript JavaScript
    52. 52. BrowserBrowser Server Webserver json JavaScript JavaScript
    53. 53. backbone.js
    54. 54. handlebars.js
    55. 55. Round 4
    56. 56. Round 4databases
    57. 57. CouchDBMongoDB+ others
    58. 58. node-mysql
    59. 59. PDO
    60. 60. mysqlnd
    61. 61. noSQL
    62. 62. Round 5
    63. 63. Round 5frameworks
    64. 64. express.js
    65. 65. var app = require(express).createServer();app.get(/, function(req, res) { res.send(hello world);});app.listen(80);
    66. 66. Silex
    67. 67. Appserver-in-PHP
    68. 68. Round 5
    69. 69. Round 5bottom line
    70. 70. PHP stillawesome
    71. 71. Node has its uses
    72. 72. Bespoke servers
    73. 73. websockets
    74. 74. Marc Gear@marcgearmarcgear@gmail.com

    ×