Giter VIP home page Giter VIP logo

react-adapter's Introduction

amphp/react-adapter

Stable License

amphp/react-adapter makes any ReactPHP library compatible with Amp v2.

Note If you're using AMPHP v3, have a look at revolt/event-loop-adapter-react instead.

Installation

This package can be installed as a Composer dependency.

composer require amphp/react-adapter

Usage

Everywhere where a ReactPHP library requires an instance of LoopInterface, you pass ReactAdapter::get() to run the ReactPHP library on Amp's event loop.

<?php

require 'vendor/autoload.php';

use Amp\Loop;
use Amp\ReactAdapter\ReactAdapter;

Loop::run(function () {
    $app = function ($request, $response) {
        $response->writeHead(200, array('Content-Type' => 'text/plain'));
        $response->end("Hello World\n");
    };

    $socket = new React\Socket\Server(ReactAdapter::get());
    $http = new React\Http\Server($socket, ReactAdapter::get());

    $http->on('request', $app);
    echo "Server running at http://127.0.0.1:1337\n";

    $socket->listen(1337);
});

You can also use the adapter to run ReactPHP apps on an Amp event loop implementation without relying on Amp's global event loop.

$loop = new Amp\ReactAdapter\ReactAdapter((new Amp\Loop\DriverFactory)->create());

react-adapter's People

Contributors

andrewmackrodt avatar kelunik avatar mmenozzi avatar trowski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-adapter's Issues

Extend documentation with warnings about etc/Factory.php override

This library does a override of React\EventLoop\Factory through autoload of etc/Factory.php. This feature is quite dangerous and can break some monorepository setup, with other services relying only on ReactPHP.

Documentation of this "feature" is not sufficient and not transparent.

It would be cool to document it and highlight it on these pages:

Thank you!

Not able to Prevent the ReactPHP's Factory Instantiation

Hi. How can I prevent ReactPHP from instantiating the React\EventLoop\Factory.

I have no const/envVar AMP_REACT_ADAPTER_DISABLE_FACTORY_OVERRIDE

PHP 7.4 Win10 & Linux

===

<?php  
require DIR . '/vendor/autoload.php';  
$loop    = Amp\ReactAdapter\ReactAdapter::get();  
$factory = new Clue\React\SQLite\Factory($loop);  
$db      = $factory->openLazy('users.db'); //<= error  
exit;  

===

"minimum-stability": "dev",  
"require": {  
    "clue/reactphp-sqlite": "dev-master",  
    "amphp/react-adapter":  "dev-master"  
}`  

===

C:\devphp\reactphp_sqlite_test>php sqlite.php
PHP Fatal error: Uncaught Error: React\EventLoop\Factory::create() has been overridden by amphp/react-adapter to prevent you from accidentally creating two event loop instances. Use Amp\ReactAdapter\ReactAdapter::get() instead of React\EventLoop\Factory::create() to ensure everything is running on the same event loop. You may set a constant or environment variable named AMP_REACT_ADAPTER_DISABLE_FACTORY_OVERRIDE to disable this protection or open an issue at https://github.com/amphp/react-adapter if you're unsure on the right way forward. in C:\devphp\reactphp_sqlite_test\vendor\amphp\react-adapter\etc\Factory.php:24
Stack trace:
#0 C:\devphp\reactphp_sqlite_test\vendor\clue\reactphp-sqlite\res\sqlite-worker.php(30): React\EventLoop\Factory::create()
#1 {main}
  thrown in C:\devphp\reactphp_sqlite_test\vendor\amphp\react-adapter\etc\Factory.php on line 24

react-adapter don't exists on packagist

Seems like you forgot to upload it to packagist react-adapter

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - The requested package amphp/react-adapter could not be found in any version, there may be a typo in the package name.

Converting Amp promise to ReactPHP promise

Hi,
I'm new to async development so this is more a question and not an issue.

I would like to create an HTTP server with https://github.com/reactphp/http which runs on the Amp loop. My HTTP server must return "Deferred Responses" (https://github.com/reactphp/http#deferred-response) because I would like to queue jobs in a Beanstalkd tube using https://github.com/amphp/beanstalk.

The problem is that I have no idea of how to do this. I'm able to create deferred responses, for example with this script:

<?php

require 'vendor/autoload.php';

use Amp\Loop;
use Amp\ReactAdapter\ReactAdapter;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Response;
use React\Http\Server;
use React\Promise\Promise;

Loop::run(function () {
    $loop = ReactAdapter::get();

    $server = new Server(function (ServerRequestInterface $request) use ($loop) {
        return new Promise(function ($resolve, $reject) use ($loop) {
            $loop->addTimer(1.5, function() use ($resolve) {
                $response = new Response(
                    200,
                    array(
                        'Content-Type' => 'text/plain'
                    ),
                    "Hello world"
                );
                $resolve($response);
            });
        });
    });

    $socket = new React\Socket\Server(8080, $loop);
    echo "Server running at http://127.0.0.1:8080\n";
    $server->listen($socket);
});

The problem comes when I want to defer a response after that the Beanstalkd client put promise resolves with a job id. The Beanstalkd's put method returns an Amp Promise but the React's HTTP server wants an instance of a React's PromiseInterface. So how can I do this? Do I have to convert Amp promise to React promise? And if so, how?

Thank you for your help!

Use the static Loop methods instead of a Driver instance

If I were to use React components alongside an interop loop in my application I think I'd want the React loop adapter to call the static Loop methods, such as Loop::onReadable(), instead of being injected with an interop driver, so that I'd have the same scoping guarantees with React components as I would with other components. Did you already consider this?

Amphp v3 compatibility

I'm using a React-based library https://github.com/jakubkulhan/bunny in my Amphp application. I noticed that this library doesn't have a v3 branch yet. Does that mean that React libraries are not yet supported in v3 or that this adapter is somehow not necessary anymore? Or is there some other issue that makes such adapter impossible?

Using amphp packages inside react's event loop.

Hello there!
As I am fairly new to async-php world, excuse me if this question is somehow out of place. I know that this package make it possible to use react-php components inside amphp event loop, however, I wonder if it is possible in reverse as well โ€“ I mean, tweaking this package to use amp components inside react loop?
As mentioned in #4, there was some package to make both world compatible โ€“ unfortunately, it is no longer available, even for a reference.
I would be grateful for any tips on this topic :-)

Can't install both amphp/amp and amphp/react-adapter

/opt/project $ composer require amphp/react-adapter amphp/amp
Using version ^1.1 for amphp/react-adapter
Using version ^2.0 for amphp/amp
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for amphp/react-adapter ^1.1 -> satisfiable by amphp/react-adapter[v1.1.0].
    - Conclusion: remove react/event-loop v0.5.2
    - amphp/react-adapter v1.1.0 requires react/event-loop ^0.4.2 -> satisfiable by react/event-loop[v0.4.2, v0.4.3].
    - Can only install one of: react/event-loop[v0.5.2, v0.4.2].
    - Can only install one of: react/event-loop[v0.5.2, v0.4.3].
    - Installation request for react/event-loop (installed at v0.5.2) -> satisfiable by react/event-loop[v0.5.2].


Installation failed, reverting ./composer.json to its original content.

This will happen even if no other packages are required!

Read / write streams

Read / write stream should replace handlers instead of appending to a list of them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.