This document contains the slides from a presentation on building asynchronous bots in PHP using AMPHP. The presentation discusses asynchronous I/O, event loops, and using AMPHP to build bots that can interact with Twitter's APIs asynchronously. It provides code examples of making asynchronous HTTP requests and processing a Twitter stream using an event loop. The presentation recommends AMPHP as a complete asynchronous framework for PHP that can be used for both event loop and fork/join parallelism in building high performance asynchronous applications.
4. @adam_englander
What You Will Need
• PHP 5.6, 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
5. @adam_englander
What We Will Discuss
• Concurrent Programming
• Asynchronous I/O
• AMPHP Asynchronous Framework for PHP
6. @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.
8. @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
9. @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
14. @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
15. 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();
16. @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
19. @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