Transcript
Page 1: Integrating Node-Js with PHP

Integrating Node.js with PHP

Lee BoyntonPHPHants March 2013 Meetup

Page 2: Integrating Node-Js with PHP

So... WTF is Node.js?

Page 3: Integrating Node-Js with PHP

Server-side JavaScript

Page 4: Integrating Node-Js with PHP

It's single threaded...

Page 5: Integrating Node-Js with PHP

...one process serves multiple clients

Page 6: Integrating Node-Js with PHP

Apache + mod_phpWebserverClients

(Example borrowed from Marc Gear's excellent server side scripting smack down)

Page 7: Integrating Node-Js with PHP

nginx + php-fpmStill pretty similar, there is a pool of available

PHP processes

Page 8: Integrating Node-Js with PHP

Node.jsWebserverClients

Page 9: Integrating Node-Js with PHP

A simple Node.js webserver

var http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(1337, '127.0.0.1');console.log('Server running at http://127.0.0.1:1337/');

Page 10: Integrating Node-Js with PHP

Why should I use Node.js then?

Page 11: Integrating Node-Js with PHP

l33tness

#1 Reason

Page 12: Integrating Node-Js with PHP

Use same language on the frontend and

backend!

Page 13: Integrating Node-Js with PHP

Websockets!!!!

Page 14: Integrating Node-Js with PHP

● Persistent connection to server via web browser

● Low latency● Bi-directional● Much better than XHR long polling (comet)

Websockets

Page 15: Integrating Node-Js with PHP

● Games● News feeds● Chat● Real-time applications

The possibilities...

Page 16: Integrating Node-Js with PHP

Awesome! Let's ditch PHP!

Or use the right tool for the right job...

Page 17: Integrating Node-Js with PHP

● 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

Reasons to use PHP still

Page 18: Integrating Node-Js with PHP

Oh yeah, the integrating part...

Memcache/redis/something else

PHP Node

Session dataSession data

Page 19: Integrating Node-Js with PHP

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"}}

Page 21: Integrating Node-Js with PHP

Quick example... (PHP)// create connection to memcached$memcached = new Memcached();$memcached->addServer('localhost', 11211);

// register handler (PHP 5.3 compatible)$handler = new Lboy\Session\SaveHandler($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';

Page 22: Integrating Node-Js with PHP

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 browser

See demo app...

Page 23: Integrating Node-Js with PHP

Conclusion...

● Using Node is fun...● Good way to add real-time functionality to

existing website● Can be used for much more


Top Related