SlideShare una empresa de Scribd logo
1 de 35
Descargar para leer sin conexión
REACT PHP
The NodeJS Challenger
LUKE KYSOW
Software Engineer @Hootsuite
Find me on Twitter @lkysow
What is React
PHP?
Written by Igor Wiedler • @igorwhiletrue
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$http = new ReactHttpServer($socket, $loop)
$http->on('request', function ($req, $rep) {
$rep->writeHead();
$rep->end("Hello World!n");
});
$socket->listen(8000);
$loop->run();
What Does PHP
Suck At?
C10K Problem
Websockets
Making lots of concurrent
requests (ex. Web Scraping)
Speed
Why does PHP
suck at solving
these problems?
Why is NodeJS
good at solving
these problems?
A typical web request
<?php
$request = $this->getRequest();
$param = $request->getParameter('param');
// these calls block
$apiResponse = $api->getSomething($param);
$dbResponse = $database->doSomething($param)
return new Response($apiResponse, $dbRespons
Latency Numbers Every
Programmer Should Know
L1 cache reference 0.5 ns
L2 cache reference 7 ns
Main memory reference 100 ns
Send 1K bytes over 1 Gbps
network
10,000 ns
Read 1 MB sequentially
from memory
250,000 ns
Round trip within same
datacenter
500,000 ns
A typical web request
<?php
$request = $this->getRequest(); // 1ns
$param = $request->getParameter('param'); //
// 100,000,000 ns
$apiResponse = $api->getSomething($param);
// 500,000 ns
$dbResponse = $database->doSomething($param)
// 1ns
return new Response($apiResponse, $dbRespons
Solution:
(╯°□°)╯︵ ┻━┻
Implement Non-Blocking I/O
and Event Loops in PHP!
Aside: Streams
$filePointer=fopen("http://google.com","rb
echostream_get_contents($filePointer,25);
//"<!doctypehtml><htmlitem"
echostream_get_contents($filePointer,25);
//"scope=""itemtype="http:/"
Aside: Streams
$filePointer = fopen("file:///tmp/test", "w"
fwrite($filePointer,
"Actual React code coming soon");
Convert this...
<?php
$request = $this->getRequest();
$param = $request->getParameter('param');
// these calls block
$apiResponse = $api->getSomething($param);
$dbResponse = $database->doSomething($param)
return new Response($apiResponse, $dbRespons
// get requests as streams
$apiStream = $api->getStream($param);
$dbStream = $database->getStream($param);
// still blocks
list($apiResp, $dbResp) =
$this->retrieveData($apiStream, $dbStream)
return new Response($apiResp, $dbResp);
$apiStream = $api->getStream($param);
$dbStream = $database->getStream($param);
// return asynchronously
$this->retrieveStreams($apiStream, $dbStre
->on('streamsReady',
function($apiResp, $dbResp) {
return new Response($apiResp, $dbRes
}
);
stream_select
$filePointer = fopen("http://google.com", "r
$readable = [$filePointer];
$write = []; $exc = []; $t = 5;
if (stream_select($readable, $write, $exc, $
if ($readable) {
$googleStream = $readable[0];
echo stream_get_contents($googleStream,
// echoes <!doctype html><html item
}
}
What do we yield control to?
An Event Loop
<?php
while (true) {
$event = $this->getNextEvent();
$event->process();
}
while (true) {
if (stream_select($read, $write, $exc, 1))
if ($read) {
foreach ($readable as $stream) {
// run callback for that stream
}
}
if ($write) {
foreach ($writable as $stream) {
// run callback for that stream
}}
}
}
Non Blocking I/O
+ Event Loops
== Awesome
== React PHP
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$socket->on('connection', function ($conn) {
$conn->write("Hello there!n");
$conn->write("Don't say anything...n");
$conn->on('data',
function ($data) use ($conn) {
$conn->close();
});
});
$socket->listen(1337);
$loop->run()
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$http = new ReactHttpServer($socket);
$http->on('request',
function ($request, $response) {
echo "I see a client!n";
$response->writeHead(200,
['Content-Type' => 'text/plain']);
$response->end("Hello World!n");
});
$socket->listen(1337);
$loop->run();
Pipes
$loop = ReactEventLoopFactory::create();
$socket = new ReactSocketServer($loop);
$socket->on('connection', function ($conn) {
$conn->pipe($conn);
}
);
$socket->listen(1337);
$loop->run();
$waiting = null;
$socket->on('connection',
function ($conn) use (&$waiting) {
if (null === $waiting) {
$waiting = $conn;
$conn->write("Wait for a partner.n");
} else {
$conn->write("Connected!");
$waiting->write("Connected!");
$conn->pipe($waiting)->pipe($conn);
$waiting = null;
}});
React Projects
React In The Industry
Web Scraping with React
High Performance Symfony
TL;DR: You'll get with this approach almost 2.000 requests/s instead of
130 on a large Symfony app.
Even if you have an "opcode cache" you have to
declare classes, instantiate your objects, read your
caches, etc for every single request. As you can surely
imagine this is very time consuming and far away
from being a perfect setup for high performance.
http://marcjschmidt.de/blog/2014/02/08/php-high-performance.html
Is React PHP a
NodeJS
Challenger?
NO
Fin
Luke Kysow • @lkysow
Please give me your feedback! http://tiny.cc/reactphp

Más contenido relacionado

La actualidad más candente

YAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービスYAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービス
Yusuke Wada
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
rajivmordani
 
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
Masahiro Nagano
 

La actualidad más candente (20)

ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
 
Perl: Coro asynchronous
Perl: Coro asynchronous Perl: Coro asynchronous
Perl: Coro asynchronous
 
How to stand on the shoulders of giants
How to stand on the shoulders of giantsHow to stand on the shoulders of giants
How to stand on the shoulders of giants
 
Asynchronous Programming FTW! 2 (with AnyEvent)
Asynchronous Programming FTW! 2 (with AnyEvent)Asynchronous Programming FTW! 2 (with AnyEvent)
Asynchronous Programming FTW! 2 (with AnyEvent)
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
 
YAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービスYAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービス
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 
Security Challenges in Node.js
Security Challenges in Node.jsSecurity Challenges in Node.js
Security Challenges in Node.js
 
Inside Bokete: Web Application with Mojolicious and others
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and others
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 
Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7Operation Oriented Web Applications / Yokohama pm7
Operation Oriented Web Applications / Yokohama pm7
 
Anyevent
AnyeventAnyevent
Anyevent
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureCréer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heure
 
ES6 is Nigh
ES6 is NighES6 is Nigh
ES6 is Nigh
 
Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)
 
NoSQL Injections in Node.js - The case of MongoDB
NoSQL Injections in Node.js - The case of MongoDBNoSQL Injections in Node.js - The case of MongoDB
NoSQL Injections in Node.js - The case of MongoDB
 
Puppet Camp 2012
Puppet Camp 2012Puppet Camp 2012
Puppet Camp 2012
 
Node.js - A Quick Tour
Node.js - A Quick TourNode.js - A Quick Tour
Node.js - A Quick Tour
 
Introduction to Nodejs
Introduction to NodejsIntroduction to Nodejs
Introduction to Nodejs
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 

Similar a React PHP: the NodeJS challenger

Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Tatsuhiko Miyagawa
 
The symfony platform: Create your very own framework (PHP Quebec 2008)
The symfony platform: Create your very own framework (PHP Quebec 2008)The symfony platform: Create your very own framework (PHP Quebec 2008)
The symfony platform: Create your very own framework (PHP Quebec 2008)
Fabien Potencier
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
 

Similar a React PHP: the NodeJS challenger (20)

The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
ReactPHP
ReactPHPReactPHP
ReactPHP
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
 
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyoエラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
エラー時にログに出力する情報と画面に表示する情報を分ける #LaravelTokyo
 
Swoole Overview
Swoole OverviewSwoole Overview
Swoole Overview
 
Generating Power with Yield
Generating Power with YieldGenerating Power with Yield
Generating Power with Yield
 
Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12Symfony components in the wild, PHPNW12
Symfony components in the wild, PHPNW12
 
ZeroMQ Is The Answer: PHP Tek 11 Version
ZeroMQ Is The Answer: PHP Tek 11 VersionZeroMQ Is The Answer: PHP Tek 11 Version
ZeroMQ Is The Answer: PHP Tek 11 Version
 
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
The symfony platform: Create your very own framework (PHP Quebec 2008)
The symfony platform: Create your very own framework (PHP Quebec 2008)The symfony platform: Create your very own framework (PHP Quebec 2008)
The symfony platform: Create your very own framework (PHP Quebec 2008)
 
PHP pod mikroskopom
PHP pod mikroskopomPHP pod mikroskopom
PHP pod mikroskopom
 
Tatsumaki
TatsumakiTatsumaki
Tatsumaki
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
Nodejsexplained 101116115055-phpapp02
Nodejsexplained 101116115055-phpapp02Nodejsexplained 101116115055-phpapp02
Nodejsexplained 101116115055-phpapp02
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 

React PHP: the NodeJS challenger