Integrating Node.js with          PHP         Lee Boynton  PHPHants March 2013 Meetup
So... WTF is Node.js?
Server-side JavaScript
Its single threaded...
...one process serves    multiple clients
Apache + mod_php         Clients                                                     Webserver    (Example borrowed from M...
nginx + php-fpmStill pretty similar, there is a pool of available                 PHP processes
Node.js      Clients   Webserver
A simple Node.js webservervar http = require(http);http.createServer(function (req, res) {  res.writeHead(200, {Content-Ty...
Why should I use Node.js        then?
#1 Reason  l33tness
Use same language on  the frontend and      backend!
Websockets!!!!
Websockets● Persistent connection to server via web  browser● Low latency● Bi-directional● Much better than XHR long polli...
The possibilities...●   Games●   News feeds●   Chat●   Real-time applications
Awesome!     Lets ditch PHP!Or use the right tool for the right job...
Reasons to use PHP still●   PHP works●   Familiarity, maturity●   Existing code in PHP●   Node still in its infancy (creat...
Oh yeah, the integrating part...                     Memcache/redis/                      something else      Session data...
However...Sessions are serialized by PHP:not|a:2:{i:0;s:4:"easy";i:1;a:1:{s:2:"to";s:5:"parse";}}Easier to parse JSON in N...
Use my session save handler...On Packagist: lboynton/memcached-json-session-save-handlerOr msgpack: https://github.com/msg...
Quick example... (PHP)  // create connection to memcached  $memcached = new Memcached();  $memcached->addServer(localhost,...
Quick example... (Node.js)1. Get session ID from cookie2. Get session data out of memcached3. Use session data to identify...
Conclusion...● Using Node is fun...● Good way to add real-time functionality to  existing website● Can be used for much more
Links● http://nodejs.org/● https://npmjs.org/● http://socket.io/● https://packagist.  org/packages/lboynton/memcached-json...
Upcoming SlideShare
Loading in...5
×

Integrating Node.js with PHP

45,995

Published on

Published in: Technology

Transcript of "Integrating Node.js with PHP"

  1. 1. Integrating Node.js with PHP Lee Boynton PHPHants March 2013 Meetup
  2. 2. So... WTF is Node.js?
  3. 3. Server-side JavaScript
  4. 4. Its single threaded...
  5. 5. ...one process serves multiple clients
  6. 6. Apache + mod_php Clients Webserver (Example borrowed from Marc Gears excellent server side scripting smack down)
  7. 7. nginx + php-fpmStill pretty similar, there is a pool of available PHP processes
  8. 8. Node.js Clients Webserver
  9. 9. A simple Node.js webservervar 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/);
  10. 10. Why should I use Node.js then?
  11. 11. #1 Reason l33tness
  12. 12. Use same language on the frontend and backend!
  13. 13. Websockets!!!!
  14. 14. Websockets● Persistent connection to server via web browser● Low latency● Bi-directional● Much better than XHR long polling (comet)
  15. 15. The possibilities...● Games● News feeds● Chat● Real-time applications
  16. 16. Awesome! Lets ditch PHP!Or use the right tool for the right job...
  17. 17. Reasons to use PHP still● PHP works● Familiarity, maturity● Existing code in PHP● Node still in its infancy (created in 2009) ○ Not as many frameworks, libraries ○ May have to write more code for some basic things ○ APIs may change, not version 1.0 yet
  18. 18. Oh yeah, the integrating part... Memcache/redis/ something else Session data Session data PHP Node
  19. 19. However...Sessions are serialized by PHP:not|a:2:{i:0;s:4:"easy";i:1;a:1:{s:2:"to";s:5:"parse";}}Easier to parse JSON in Node.js:{"this": {"is": "easier"}}
  20. 20. Use my session save handler...On Packagist: lboynton/memcached-json-session-save-handlerOr msgpack: https://github.com/msgpack/msgpack-php● ini_set(session.serialize_handler, msgpack)
  21. 21. Quick example... (PHP) // create connection to memcached $memcached = new Memcached(); $memcached->addServer(localhost, 11211); // register handler (PHP 5.3 compatible) $handler = new LboySessionSaveHandler($memcached); session_set_save_handler( array($handler, open), array($handler, close), array($handler, read), array($handler, write), array($handler, destroy), array($handler, gc) ); register_shutdown_function(session_write_close); session_start(); // start using the session $_SESSION[serialisation] = this should be in json;
  22. 22. Quick example... (Node.js)1. Get session ID from cookie2. Get session data out of memcached3. Use session data to identify client and send relevant info to their browserSee demo app...
  23. 23. Conclusion...● Using Node is fun...● Good way to add real-time functionality to existing website● Can be used for much more
  24. 24. Links● http://nodejs.org/● https://npmjs.org/● http://socket.io/● https://packagist. org/packages/lboynton/memcached-json- session-save-handler● https://github.com/msgpack/msgpack-php● https://github.com/msgpack/msgpack-node
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×