Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Christopher Pitt:
Building a blog - Introduction & Posts
Oct 29, 2018 @ 17:56:15

Christopher Pitt has kicked off a new series of posts to his site covering the creation of a new blog system. It's not just any blogging system, however. In this system he's developing it as an asynchronous application using preprocessing. In the introduction to the project he fills in more of the details:

I’ve been building this blog, for a few days now, and it’s been a fun experience. This is partly because it’s an asynchronous application, and partly because it uses a lot of preprocessing.

I thought it would be interesting for me to describe how it is put together, in an ad-hoc sort of series. In the series, I’ll show bits of code and links to libraries I’ve used. I’ll talk about why I’ve chosen to do things in certain ways, and what I could improve (short of deleting everything and starting over with a mature framework).

In this first part of the series, he includes a "tiny bit of code" showing a component of the system before the preprocessing has been run. He's also posted the second part of the series that continues covering the functionality for handling "posts" in more depth (including the use of flat files for content storage and functionality to output Markdown content as HTML).

tagged: tutorial series part1 part2 blog introduction asynchronous preprocessing

Link: https://assertchris.io/post/2018-10-24-building-something-new

Sergey Zhuk:
Sending Email Asynchronously With ReactPHP Child Processes
May 04, 2018 @ 14:42:27

Sergey Zhuk has a new tutorial posted on his site showing you how to use child processes in ReactPHP to send emails asynchronously using Swiftmailer.

In PHP the most of libraries and native functions are blocking and thus they block an event-loop. For example, each time we make a database query with PDO, or check a file with file_exists() our asynchronous application is being blocked and waits. Things often become challenging when we want to integrate some synchronous code in an asynchronous application. This problem can be solved in two ways: rewrite a blocking code using a new non-blocking one or fork this blocking code and let it execute in a child process, while the main program continues running asynchronously.

This first approach is not always available, asynchronous PHP ecosystem is still small and not all use-cases have asynchronous implementations. So, in this article, we will cover the second approach.

He starts by creating the main HTTP server handler running locally on port 8080. He adds in an exception handler to catch potential issues and provides example code of an exception being thrown. With that structure in place he starts on the Swiftmailer integration, adding it to the exception handler and pushing the details of the exception into the message body. This is then modified to use the react/child-process package to wrap a new PHP file inside of a child process loop. The tutorial ends with an example of how to pass data between the parent and child process. In this case it's the message from the exception.

tagged: send email child process reactphp tutorial exception asynchronous

Link: http://sergeyzhuk.me/2018/05/04/reactphp-child-processes/

Zend Framework Blog:
Async Expressive? Try Swoole!
Mar 22, 2018 @ 14:28:22

On the Zend Framework blog there's a new tutorial posted that wants to help you use Expressive for asynchronous request handling with the help of the Swoole extension.

When we were finalizing features for Expressive 3, we had a number of users testing using asynchronous PHP web servers. As a result, we made a number of changes in the last few iterations to ensure that Expressive will work well under these paradigms.

Specifically, we made changes to how response prototypes are injected into services.

The article starts by talking about the issue with the previous response prototype handling, mostly that, in an async world, all responses would be sharing the same instance rather than rebuilding a new one for each request. It then explains their reasoning behind even worrying about async support in the framework's response handling noting that the major reason had to do with the performance gain. The post mentions the Swoole extension and shows how to install it via PECL but you'll need to check out the project's documentation to see how to create a server that makes use of it.

tagged: asynchronous swoole extension zendframework zendexpressive v3

Link: https://framework.zend.com/blog/2018-03-21-expressive-swoole.html

Cees-Jan Kiewiet:
ReactPHP with RecoilPHP: An introduction
Feb 05, 2018 @ 15:51:01

In a new post to his site Cees-Jan Kiewiet has posted in introduction to using asynchronous processing in your PHP application by using RecoilPHP and ReactPHP.

Getting your mind wrapped around async nature can be mind bending at first. But with RecoilPHP you can write code promise as if you're writing sync code.

He starts with some sample code showing the difference between normal ReactPHP and how the same kind of thing would be written using RecoilPHP. He then gets into the setup of a project that includes the RecoilPHP package and several others from React. With that base set up, he shows how to create a promise that opens a socket and listens on it for incoming messages and how to modify it to add additional coroutines. Finally he shares a few "bonus tips" and covers error handling.

tagged: recoilphp reactphp tutorial introduction asynchronous processing promise

Link: https://blog.wyrihaximus.net/2018/02/reactphp-with-recoilphp/

Sergey Zhuk:
Asynchronous PHP: Why?
Feb 02, 2018 @ 17:15:38

In a post to his site Sergey Zhuk shares his opinion on asynchronous PHP and why it's in such demand these days in web-based applications.

Asynchronous programming is on demand today. Especially in web-development where responsiveness of the application plays a huge role. No one wants to waste their time and to wait for a freezing application, while you are performing some database queries, sending an email or running some other potentially long-running tasks.

Users want to receive responses to their actions, and they want these responses immediately. When your application becomes slow, you start losing your clients. Once a user has to deal with a freezing application, in most cases he or she simply closes it and never returns. When the UI freezes from the user’s point of view, it is not clear if your application is broken, or it is performing some long-running task and requires some time for it.

He goes on to talk about the importance of responsiveness in web applications and clearing up the difference between running tasks in parallel and running them asynchronously. He also talks briefly about the use of asynchronous processing on the backend and how it compares to other technology (like Node.js and Go) that have built-in asynchronous processing.

tagged: asynchronous processing responsive parallel backend language

Link: http://sergeyzhuk.me/2018/02/02/why-asynchronous-php/

Sergey Zhuk:
Building ReactPHP Memcached Client: Unit-Testing Promises
Nov 21, 2017 @ 17:43:32

Sergey Zhuk has posted the latest part of his "Building a ReactPHP Memcache client" series to his site today. In this latest article, part four of the series, he focuses on unit testing the client as he's developed it so far.

This is the last article from the series about building from scratch a streaming Memcached PHP client for ReactPHP ecosystem. The library is already released and published, you can find it on GitHub. In the previous article, we have completely finished with the source code for async Memcached ReactPHP client. And now it’s time to start testing it.

He then walks through some of the steps to create the tests for the client, made a little more difficult by its asynchronous handling. He shows how to use Mockery to create tests that evaluate the results of the promises from the client, starting with a simple check on the return of a version call. The post goes on to show testing for other parts of the client and includes all of the code and commands you'll need to execute them in your own environment.

tagged: reactphp memcached client asynchronous tutorial series part4

Link: http://sergeyzhuk.me/2017/11/20/memcached-reactphp-p4/

Sergey Zhuk:
Cancelling ReactPHP Promises With Timers
Aug 31, 2017 @ 17:08:35

Sergey Zhuk has a new post to his site continuing on from a previous article about promises in ReactPHP. In this latest post he shows how you can use timers to cancel the execution of promises if their execution time grows too large.

At first, let’s refresh in memory what is Promise. A promise represents a result of an asynchronous operation. You can add fulfillment and error handlers to a promise object and they will be invoked once this operation has completed or failed. Check this article to learn more about promises.

Promise is a very powerful tool which allows us to pass around the code the eventual results of some deferred operation. But there is one problem with promises: they don’t give us much control.

He then shares the code required to make a simple "Hello world" promise and a handler for when it's cancelled. He then shows how to use the PromiseTimer functionality in ReactPHP to set a timeout on the promise instance and automatically call the cancellation event when it reaches that limit. He also includes a reminder that, in the cancellation handling of the promise it's a good idea to close out and cancel any other resources that may have been in use. The post ends with an example of how you can stack other functionality on the timeout using methods like "then" and "otherwise".

tagged: reactphp timer promise promisetimer tutorial asynchronous

Link: http://seregazhuk.github.io/2017/08/22/reactphp-promise-timers/

Lorna Mitchell:
Handling Incoming Webhooks in PHP
Jul 27, 2017 @ 17:27:14

Lorna Mitchell has a quick post to her site sharing a method she uses for handling incoming web hooks requests in PHP and the process her code usually uses for parsing the incoming message.

An increasing number of applications now offer webhooks as an integration, often in addition to an API. The classic example, familiar to most developers, is the GitHub webhooks which can notify your other systems such as CI tooling that a new commit has been added to a branch.

[...] Whether it's your source control, updates from your IoT sensors, or an event coming from another component in your application, I have some Opinions (TM) about handling webhooks, so I thought I'd write them down and include some code as well, since I think this is an area that many applications will need to work with.

She talks about the receive/respond workflow she recommends: immediately storing and acknowledge the data and then responding out of band (asynchronously). She includes a bit of example code that reads in the raw input from the incoming message, saves it and then responds back with a 200 response code back to the waiting service. She then talks about the out-of-band processing the message could use, evaluating the contents and acting on them as a result.

tagged: webhooks incoming processing asynchronous response tutorial

Link: https://lornajane.net/posts/2017/handling-incoming-webhooks-in-php

SitePoint PHP Blog:
How to Scan Fingerprints with Async PHP and React Native
Jun 07, 2017 @ 16:52:09

The SitePoint PHP blog has posted a tutorial from Christopher Pitt with another interesting use of PHP: using it and React to scan fingerprints to aid in multi-factor authentication.

We live in interesting times. A short while ago, a company called OfferZen announced a new, programmable credit card. It’s been a long time since I was this excited to get my hands on a piece of tech. My mind has been brimming with ideas ever since.

So, I decided to write about one of them!

I’m going to describe the process of building custom multi-factor authentication for all transactions. I don’t want to do the usual (and boring on its own) SMS or push notification one-time-password stuff. I want to build a fingerprint scanner right into my phone. [...] In this tutorial, we’re going to look at how to set up a simple iOS app using React Native. We will also set up an asynchronous HTTP server, with a web socket connection to the app.

He starts by taking some time to introduce React Native for those not familiar with it and what it brings to the table. He then walks through the installation of React Native and how to install the TouchID package using yarn. He also includes some simple JS code you can use to ensure it's all working. Next up he uses PHP to create an asynchronous server with the amphp/aerys package (this code). He creates the server with a "scan" endpoint handlers for opening the connection, performing the handshake and reacting to incoming data. Then he brings them together, connecting React and PHP, and sets up a script to wait for fingerprints. Code is included to perform the validation of the fingerprint and the end result should look something like this.

tagged: react reactnative fingerprint scanner mobile asynchronous tutorial

Link: https://www.sitepoint.com/scan-fingerprints-async-php-react-native/

SitePoint PHP Blog:
Writing Async Libraries – Let’s Convert HTML to PDF
Feb 21, 2017 @ 15:58:05

The SitePoint PHP blog has another tutorial posted from author Christopher Pitt looking at writing async libraries with PHP. In this particular article he focuses on just one of many tasks an asynchronous library could perform: converting HTML to PDF documents.

I can barely remember a conference where the topic of asynchronous PHP wasn’t discussed. I am pleased that it’s so frequently spoken about these days. There’s a secret these speakers aren’t telling, though: "Making asynchronous servers, resolving domain names, interacting with file systems: these are the easy things. Making your own asynchronous libraries is hard. And it’s where you spend most of your time!"

The reason those easy things are easy is because they were the proof of concept – to make async PHP competitive with NodeJS. [...] Today, we’re going to look at a few ways to make your application code work well in an asynchronous architecture. Fret not – your code can still work in a synchronous architecture, so you don’t have to give anything up to learn this new skill. Apart from a bit of time…

He starts with some theory about things in the async world including callbacks, promises and what they might look like in PHP-land. He then starts in on the creation of the PDF files, creating a "Driver" class to handle some of the logic and using the Dompdf library to do the heavy lifting (the conversion from HTML to PDF). He walks through the code required for this class then moves on to the code, using the Amp project, to handle the async operations. He then creates a simple set of web accessible endpoints that call the Driver class with some basic attributes and performing the conversion. He ends the post talking about porting the parallel driver to other systems (such as ReactPHP) and a few simple steps if you need to move back to the synchronous world.

tagged: asynchronous conversion dompdf html pdf tutorial amp

Link: https://www.sitepoint.com/writing-async-libraries-lets-convert-html-to-pdf/


Trending Topics: