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

Sergey Zhuk:
Build A Simple Chat With ReactPHP Socket: Server
Jun 28, 2017 @ 10:46:39

Sergey Zhuk has a new post to his site showing how you can use ReactPHP's socket component to build a simple chat service as a server runing on a remote port.

In this article, we are going to build a simple chat server based on ReactPHP Socket Component. With this component, we can build simple async, streaming plaintext TCP/IP or a secure TLS socket server.

There are client and server sockets. The server is bound to a specific port number and just waits listening on this port. The client knows the host of the server and the port on which the server is listening. When the connection between server and client is established, the data exchange begins.

He then gets into the code required to make the server and add in some additional functionality. He starts by creating the server to listen for incoming connections and has it write back a simple message to prove it's working correctly. He then moves on to the code required for sending and receiving data. This initial version just echoes back what the user submits. He takes this an expands it out to start on the intiial steps of the chat system - creating the connection pool for multiple clients, storing usernames of those connected and using that information when transmitting a message to all clients connected.

tagged: simple reactphp chat server tutorial server client username

Link: http://seregazhuk.github.io/2017/06/22/reactphp-chat-server/

Loïc Faugeron:
Super Speed Symfony - ReactPHP
Apr 14, 2016 @ 09:10:57

Loïc Faugeron, author of the recent "Ultimate Guide" to Symfony components series has a new kind of post to his site today. In this latest article he shows you how to integrate Symfony and ReactPHP for "super speed Symfony" sites.

HTTP frameworks, such as Symfony, allow us to build applications that have the potential to achieve Super Speed.

A first way to make use of it is to run our application as a HTTP server. In this article we'll take a Symfony application and demonstrate how to run it as HTTP server using ReactPHP.

He walks you through the installation of the ReactPHP HTTP server and provides some simple code to get a server up and running. He uses this to provide a "Hello world" example and helps you test it to be sure everything's up and running as expected. He then creates a basic Symfony application that uses the HttpFoundationRequest handling to wrap that same server and yield the same results. He includes some benchmarking examples and some updates he needed to make to have it work correctly with the Blackfire.io profiling service. He ends the post with a look at some of the alternatives to ReactPHP (including IcicleIO, Aerys and PHP FastCGI), why it improves performance and how to make it production ready with [] Supervisord(http://supervisord.org/).

tagged: symfony reactphp tutorial introduction server performance alternatives

Link: https://gnugat.github.io/2016/04/13/super-speed-sf-react-php.html

Cees-Jan Kiewiet:
ReactPHP: HTTP Client
Nov 05, 2015 @ 12:05:14

Cees-Jan Kiewiet has a post on his site focusing on the HTTP client side of the functionality offered by the ReactPHP. In this post he covers the basics of installation and usage with plenty of code examples (and screencasts of it in action).

Aside from a HTTP component ReactPHP also has a HTTP Client component that lets your send out HTTP requests. It is incredibly handy when you need to communicate with for example elasticsearch's REST API, AWS platform through their SDK or the RIPE Atlas API.

He walks you through the simple installation of the library (via Composer) and the code to send a simple request to an example.com domain, returning the HTML contents of the page. He then gets to some more complex examples: sending two requests at the same time, streaming the response body as it arrives and an example based on community feedback - streaming Twitter data. He ends the post with a community example showing the use of the Buzz HTTP client to make simple requests.

tagged: reactphp http client example stream twitter screencast

Link: http://blog.wyrihaximus.net/2015/11/reactphp-http-client/

SitePoint PHP Blog:
An Introduction into Event Loops in PHP
Sep 10, 2015 @ 10:06:55

The SitePoint PHP blog has posted a tutorial from Christopher Pitt introducing you to using event loops in PHP, a feature that allows for asynchronous processing, executing code while waiting for other code to finish.

PHP developers are always waiting for something. Sometimes we’re waiting for requests to remote services. Sometimes we’re waiting for databases to return rows from a complex query. Wouldn’t it be great if we could do other things during all that waiting?

If you’ve written some JS, you’re probably familiar with callbacks and DOM events. And though we have callbacks in PHP, they don’t work in quite the same way. That’s thanks to a feature called the event loop.

He starts by explaining event loops with an example from a language that naturally supports it - Javascript. He includes another example using the setTimeout function in Javascript to show a simple loop but points out that PHP just doesn't support this same kind of handling (code included). So, how can you simulate the loop like in JS? He links to and includes a examples of two libraries that could be dropped in and used to do the hard work behind the scenes of the looping:

He suggests that PHP developer "get out of the single threaded mindset" and work in ways to handle asynchronous processing into their code to improve performance and flexibility.

tagged: event loop javascript library icicle reactphp asynchronous processing

Link: http://www.sitepoint.com/an-introduction-into-event-loops-in-php/

Cees-Jan Kiewiet:
AWS PHP SDK Asynchronously
Jun 30, 2015 @ 11:31:15

Cees-Jan Kiewiet has a new post today talking about some interesting trickery he was able to do with the AWS (Amazon Web Services) PHP SDK to allow requests to be made asynchronously.

Just got off the AWS SDK for PHP Office Hour hangout and it was great talking with both team members Jeremy and Michael. And one of the things we talked about was async access to the AWS services using the PHP SDK. The goal of this post is to get the AWS PHP SDK client working asynchronously.

He starts with brief instructions on getting the SDK installed (via Composer) along with a library of his own that brings in a few other dependencies. The ReactPHP event loop is what makes the asynchronous connections possible. He includes the code to create the new handler stack and how to use it to make the asynchronous calls. A demo screencast is also included in the post to illustrate the output from a simple set of requests.

tagged: aws amazon sdk asynchronous connection reactphp event loop tutorial

Link: http://blog.wyrihaximus.net/2015/06/aws-php-sdk-asynchronously/

Thomas Weinert:
Building A Sensor Phalanx With PHP
Nov 18, 2013 @ 09:15:12

Thomas Weinert continues his series looking at merging the physical and virtual using PHP to with messages coming from servers and how to build it into a sensor phalanx.

Sensors are fun. They report from the physical world into the digital. But getting the signal into php is only the first part, you will have to get them out again. This post shows how to get data from analog sensors pushed to the browser. It uses Carica Chip, if you haven't read my previous blog post you should do it first.

He talks about the use of ReactPHP in the Carica library and the addition of Ratchet to help with the websocket messaging. He sets up two simple servers - one HTTP, the other websocket - to handle the reporting and interaction with the sensor. He includes the code for the HTTP server and uses his example code to make the phalanx listening on 8081. Some other code is included to make the listeners and the simple UI for the charts. A video is included showing it all in action, reacting to a moving light.

tagged: sensor phalanx tutorial carica chip library reactphp ratchet

Link: http://www.a-basketful-of-papayas.net/2013/11/building-sensor-phalanx-with-php.html

Phil Sturgeon:
Benchmarking Codswallop: NodeJS v PHP
Nov 12, 2013 @ 09:21:29

Phil Sturgeonhas posted about some Node.js vs PHP benchmarks that someone linked him to concerning web scraping. The article suggests that Node.js "owns" PHP when it comes to this but, as Phil finds out, there's a bit more to the story than that.

Sometimes people link me to articles and ask for my opinions. This one was a real doozy. Oh goody, a framework versus language post. Let's try and chew through this probable linkbait [where] we're benchmarking NodeJS v PHP. Weird, but I'll go along with it. Well, now we're testing cheerio v PhpQuery which is a bit different, but fine, let's go along with it.

Through a little discovery, Phil noticed phpQuery using file_get_contents, a blocking method for fetching the remote pages to scrape. Node.js instead uses a non-blocking method, meaning multiple files can be fetched at the same time. In answer to this blocking vs non-blocking, he decided to run benchamrks against a few cases - Node.js/Cherrio, PHP/phpQuery and his own, more correct comparison to the Node option - PHP/ReactPHP/phpQuery. He's shared his results, showing a major difference between the straight phpQuery and the React-based version.

It seems likely to me that people just assume PHP can't do this stuff, because by default most people arse around PHP with things like MAMP, or on their shitty web-host where is is hard to install things and as such get used to writing PHP without utilizing many extensions. It is probably exactly this which makes people think PHP just can't do something, when it easily can.
tagged: nodejs reactphp webpage scraping benchmark compare

Link: http://philsturgeon.co.uk/blog/2013/11/benchmarking-codswallop-nodejs-v-php