The document discusses integrating Node.js with PHP by sharing session data stored in Memcached or Redis. It provides examples of using a Memcached session handler in PHP that serializes session data to JSON, and an example Node.js app that retrieves session data from Memcached to identify users. While Node.js enables real-time features, the document argues that PHP still has advantages for many use cases and that the right tool should be chosen for the job.
16. Awesome!
Let's ditch PHP!
Or use the right tool for the right job...
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. Oh yeah, the integrating part...
Memcache/redis/
something else
Session data Session data
PHP Node
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. Use my session save handler...
On Packagist: lboynton/memcached-json-
session-save-handler
Or msgpack: https://github.
com/msgpack/msgpack-php
ini_set('session.serialize_handler', 'msgpack')
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. Quick example... (Node.js)
1. Get session ID from cookie
2. Get session data out of memcached
3. Use session data to identify client and send
relevant info to their browser
See demo app...
23. Conclusion...
Using Node is fun...
Good way to add real-time functionality to
existing website
Can be used for much more