Asynchronous software development is rapidly moving from the niche to the mainstream. That mainstream now includes PHP. This workshop will give you hands on instruction in building an asynchronous application in PHP. We'll build a Twitter Bot utilizing the Amp concurrency framework for PHP and the Twitter Streaming API. During this time you'll learn the basics regarding the Amp event loop, generators and co-routines, and writing non-blocking code. Get ready for the future of PHP today.
2. @adam_englander
What You Will Need
• PHP 7.0, or 7.1 (Examples will be 7.1)
• intl extension
• Twitter account
• Twitter Developer App: https://apps.twitter.com
• OAuth Credentials: https://dev.twitter.com/oauth/
overview/application-owner-access-tokens
3. @adam_englander
What We Will Discuss
• Concurrent Programming
• Asynchronous I/O
• AMPHP Asynchronous Framework for PHP
4. @adam_englander
What We Will Do
1. We will use an asynchronous HTTP client to
execute a Twitter REST API command.
2. We will use an asynchronous HTTP client to
process a continuous stream via the Twitter
User Stream API.
3. We will leverage both of the above to listen to a
topic and automatically respond to updates.
6. @adam_englander
Forking and Threading
• Available in PHP core since PHP 4.1
• Requires the use of shared memory
• Requires code to be written for management of
forks/threads
• Creates separate copies of code for each
thread/fork
7. @adam_englander
Asynchronous I/O
• Frees up the current process while I/O is
performed
• Executes code based on I/O events
• No copying of code for separate process
12. @adam_englander
Event Loop Parallelism
P R O C E S S
Q U E U E
I T E M
Q U E U E
I T E M
I N
Q U E U E ?
X
Yes
No
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
13. Event Loop Example
<?php
# Build objects to handle asynchronous interaction
$loop = ReactEventLoopFactory::create();
$dnsFactory = new ReactDnsResolverFactory();
$dns = $dnsFactory->createCached('8.8.8.8', $loop);
$factory = new ReactHttpClientFactory();
$client = $factory->create($loop, $dns);
$resp = array();
# Create callback for handling response
$responseHandler = function ($response) use ($resp) {
$response->on( 'data', function ($data) use ($resp) {
$resp[] = $data;
});
};
# Queue up requests to send
$request = $client->request('GET', 'http://www.timeapi.org/utc/now');
$request->on('response', $responseHandler);
$request->end();
$request = $client->request('GET', 'http://www.timeapi.org/utc/now');
$request->on('response', $responseHandler);
$request->end();
# Run the loop
$loop->run();
14. @adam_englander
Fork/Join vs Event Loop
• Enhance existing
synchronous apps
• Promises
• Hack/HHVM
• Non-HTTP apps
• Full async apps
• High volume apps
Fork/Join Event Loop
17. @adam_englander
Complete Modern Framework
• Modular framework segmented into logical parts
• Supersedes icicle.io
• Drivers for common storage engines
• Can be used for event loop and fork/join
• Can use threads or async i/o