Blog de programación, errores, soluciones

Chose Language:
comments
Author: Admin/Publisher |finished | checked

ReactPHP an instresting library for PHP

Today we will see an introduction to a very interesting library for PHP called ReactPHP. This is a low-level library based on events and non-blocking I/O that allows us to:

  • Stream abstraction
  • Asynchronous DNS resolver
  • Network client/server
  • HTTP client/server
  • Process interaction

Some Concepts to Understand

In itself, we have many concepts to understand here, not assuming that just because we have heard them before we know what certain topics are about.

Non-blocking I/O

The term Non-blocking I/O refers to a technique in which a program does not wait for an input or output operation to finish before continuing with another task. This technique can be useful for any application that needs to handle multiple connections simultaneously and be able to respond even while waiting for external data.

For example, in a chat application, we do not need to wait for someone to write to us in order to write ourselves, nor would it be efficient to constantly check if someone has written. The best approach would be to use bidirectional websockets, which utilize socket addresses for efficient communication.

Streams

Streams in ReactPHP allow you to efficiently process large amounts of data (such as downloading a multi-gigabyte file) in small chunks without having to store everything in memory at once

I’m not sure if this is a good analogy, but we can say that ReactPHP streams are like when we send a large file over a network that gets segmented and sent in packets

Async DNS resolver

This can be quite beneficial. Consider that your website may need to make a request to another website but also needs to continue asynchronously with its ongoing tasks

This isn’t a DNS server from what I understand; if you’re looking for a DNS server, you have BIND9.

Network Client/Server

offers a comprehensive toolkit for building network servers in PHP. In addition to handling asynchronous network clients.

For example: You can create network clients that can send requests to remote servers asynchronously.

Main Protocols supported: HTTP, WebSockets, TCP, UDP, DNS, custom network protocols using sockets

HTTP Client/Server

En ReactPHP, you can create HTTP server clients; therefore, you can have your website online without having to depend on Apache or Nginx. In fact, an example of this is provided in the introduction of the official page

<?php

// $ composer require react/http react/socket # install example using Composer
// $ php example.php # run example on command line, requires no additional web server

require __DIR__ . '/vendor/autoload.php';

$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
    return React\Http\Message\Response::plaintext(
        "Hello World!\n"
    );
});

$socket = new React\Socket\SocketServer('127.0.0.1:8080');
$http->listen($socket);

echo "Server running at http://127.0.0.1:8080" . PHP_EOL;

Regarding how to set up the server and bring it down when we need it, I have no idea. I suppose it will be with a process or with a daemon. Without a doubt, this is much lighter than an Apache server, although it is not as robust.

The HTTP server of ReactPHP is more aimed at real-time applications such as chat, notifications, and others

interaction with processes

It’s the ability to communicate and coordinate the execution of multiple processes asynchronously and efficiently.

This way, you can execute processes, even if some haven’t finished.

What can i do with ReactPHP?

We have already seen some things about the features of ReactPHP, but what can I do with it?

  • Web servers
  • Real-time applications
  • Microservices
  • RESTful API services
  • Developing proxies and load balancers

As an interesting example, we can see a chat created with this technology at https://github.com/Ahmard/reactphp-live-chat.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments