0% found this document useful (0 votes)
958 views

Integrating Node-Js With PHP

This document discusses integrating Node.js with PHP by sharing session data stored in Memcached or Redis. It presents examples of using a Memcached session handler in PHP that serializes session data to JSON for easier parsing in Node.js. Benefits of Node.js like real-time capabilities and using the same language on frontend and backend are discussed, as well as reasons one may still prefer to use PHP for certain tasks.

Uploaded by

volution89
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
958 views

Integrating Node-Js With PHP

This document discusses integrating Node.js with PHP by sharing session data stored in Memcached or Redis. It presents examples of using a Memcached session handler in PHP that serializes session data to JSON for easier parsing in Node.js. Benefits of Node.js like real-time capabilities and using the same language on frontend and backend are discussed, as well as reasons one may still prefer to use PHP for certain tasks.

Uploaded by

volution89
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Integrating Node.

js with
PHP
Lee Boynton
PHPHants March 2013 Meetup

So... WTF is Node.js?

Server-side JavaScript

It's single threaded...

...one process serves


multiple clients

Apache + mod_php
Clients

Webserver

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

nginx + php-fpm
Still pretty similar, there is a pool of available
PHP processes

Node.js
Clients

Webserver

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/');

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 polling (comet)

The possibilities...

Games
News feeds
Chat
Real-time applications

Awesome!
Let's 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 (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

Oh yeah, the integrating part...

Memcache/redis/
something else

Session data

PHP

Session data

Node

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

Use my session save handler...


On Packagist: lboynton/memcached-jsonsession-save-handler
Or msgpack: https://github.
com/msgpack/msgpack-php
ini_set('session.serialize_handler', 'msgpack')

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';

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...

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-jsonsession-save-handler
https://github.com/msgpack/msgpack-php
https://github.com/msgpack/msgpack-node

You might also like