RxPHP / RxHttp

Http Client for RxPHP
MIT License
22 stars 2 forks source link

Sending request using ReactPHP event loop #9

Closed unhappyby closed 4 years ago

unhappyby commented 5 years ago

When I try to use RxHttp with ReactPHP loop then sending request do not return anything on subscribe. Please, can you help me?

Code to reproduce:

<?php

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

use React\EventLoop\Factory;
use Rx\React\Http;
use Rx\Scheduler;

loop = Factory::create();
Scheduler::setDefaultFactory(function () use ($loop) {
    return new Scheduler\EventLoopScheduler($loop);
});

register_shutdown_function(function () use ($loop) {
    $loop->run();
});

$request = Http::get('https://jsonplaceholder.typicode.com/todos/1');

$request->subscribe(
    function ($some) {
        echo 'progress' . $some;
    },
    function ($some) {
    },
    function () {
        echo 'completed';
    });

Composer json:

{
    "name": "unhappyby/rxphp",
    "require": {
        "reactivex/rxphp": "^2.0",
        "react/event-loop": "^1.1",
        "rx/http": "^2.1"
    }
}
davidwdan commented 5 years ago

The library uses a react event loop internally. If you want to override it with your own instance, use:

\EventLoop\setLoop($loop);

If you want to make it work with your example, you need to use Scheduler::setAsyncFactory instead of Scheduler::setDefaultFactory.