Async PHP

Applications

Hi, I'm Chris

Let's start

with a website...

composer require
"amphp/aerys:^0.7.4"

Creating routes

server.php

Using different methods and paths

server.php

Creating a host

server.php

Creating a public folder

server.php

vendor/bin/aerys -d -c server.php

What about when a route changes?

composer require --dev "yosymfony/resource-watcher:^1.2" 

Creating a finder

scripts/watch

Creating a watcher

scripts/watch

Acting on changes

scripts/watch

Controlling processes easily...

Starting processes

helpers.php

Identifying processes

helpers.php

Stopping processes

helpers.php

Restarting the server

scripts/watch

How does this help us?

Creating responders

app/Responder/HomePageResponder.php

Creating invocable route handlers

server.php

How is this different

to my favourite framework?

Machines can execute code in different ways...

They can execute synchronously,

They can execute asynchronously,

They can execute

in parallel.

We write asynchronous code using generators!?

Let's talk about it...

We have arrays...

We have array-like things...

We have iterators...

Generators are an easy way to produce iterator-like objects

Sometimes they help to

save memory

Sometimes they help to

write cleaner code

They generate new data on-demand

What if we use them to generate new code

on-demand?

Back to the website...

Creating an HTML template

resources/views/register.html

Returning a template

server.php

Getting posted values

server.php

Let's make this reusable...

Making a helper function

helpers.php

Using the helper function

server.php

composer require
"league/plates:^3.3" 
composer require
"amphp/parallel-functions:^0.1.2" 

Designing a helper function

helpers.php

Using the helper function

server.php

Let's talk to the database...

composer require
"amphp/mysql:^0.3.1" 

Connecting to the database

helpers.php

Executing prepared statements

helpers.php

Using these helper functions

server.php

Let's talk specifics!

The Filesystem library uses EIO, Parallel,

or blocking

The Parallel Functions library uses Parallel

The Parallel library uses PThreads or Process Control

The MySQL library uses

a custom client

All of these return

Amp promises

There's a bunch of boilerplate, though...

composer require
"pre/async:^0.8.0" 

Good code

helpers.php

Better code

helpers.pre

composer require
"pre/short-closures:^0.6.0" 

Good code

helpers.php

Better code

helpers.pre

Enabling these...

server.php

There's a lot more to this preprocessing stuff

longer talk: https://youtu.be/0AZRp_8TT0U

What about WebSockets?!

Accepting connections

server.php

Connecting to the server

server.php

Connect to the server from Javascript

What about testing?!

Use PHPUnit,

When you need to

test promises, use

Amp\wait($promise)

You can even duplicate the server watcher to run tests after changes

What about hosting?!

Forwarding requests from Nginx

Thanks

 

twitter.com/assertchris
slides.com/assertchris

Async PHP Applications

By Christopher Pitt

Async PHP Applications

You've probably been to a couple talks, about how to do "asynchronous things" with PHP. What the experts don't tell you is that the hard part starts when you need to build an application. Sure, there are tools for starting an HTTP server or connecting to a web socket. But what about when you need to write to a database or send an email. You go searching for good tutorials, and all you get is API documentation; about how to use each component in isolation. Say goodbye to that frustration. In this workshop, I'm going to show you how to build an entirely asynchronous, high-concurrency PHP application. We'll bring those isolated components together, to build a real application.

  • 2,551