React PHP timeout always resolves - php

I was playing around with React and wanted to try to get a working timeout function. Following (sort of) the examples and Unit tests from https://github.com/reactphp/promise-timer#timeout I came up with:
use React\Promise\Timer;
$promise = uncertainOperation();
$loop = \React\EventLoop\Factory::create();
Timer\timeout($promise, 1, $loop)->then(
function ($value) {
var_dump($value);
}
,
function ($error) {
var_dump($error);
}
);
$loop->run();
function uncertainOperation() {
return new React\Promise\Promise(
function ($resolve) {
for($i = 0; $i < 30000000; $i++) { }
$resolve("Done");
}
);
}
But this always resolves with "Done" no matter how low I set the time in Timer\timeout. What am I missing?

The issue with your code is that it blocks. And it synchronously resolves the promise. It does nowhere return to the event loop driver, so that it could schedule the timeout watcher.
Try changing your code to use a timeout as simulation of e.g. a network timeout.
function uncertainOperation($loop) {
return new React\Promise\Promise(
function ($resolve) use ($loop) {
$loop->addTimer(5, function () {
$resolve("Done");
});
}
);
}
$loop->run();
Unfortunately, you have to pass around the loop in React.

Related

How send message on swoole (websocket extension for php) from php to browser?

This file run my server
<?php
class websocket
{
public $ws;
public function start()
{
$this->ws = new swoole_websocket_server('127.0.0.1', 9502);
$this->ws->on('open', function ($ws, $request) {
echo "connection open: {$request->fd}\n";
});
$this->ws->on('message', function ($ws, $frame) {
echo "received message: {$frame->data}\n";
$this->ws->push($frame->fd, json_encode(["hello", "world"]));
});
$this->ws->on('close', function ($ws, $id) {
$this->onClose($id);
});
$this->ws->start();
$this->sendMessage(1, "asdfasdf");
}
public function sendMessage($id,$msg)
{
$this->ws->push($id, "asdf");
}
}
I run it from cli like this:
php -r 'include("websocket.php"); $web = new websocket; $web->start();'
then I open on browser this file
<?php
include ('websocket.php');
$n = new websocket();
$n->ws->push(1, "asdf", 1, true);
and I get this error:
127.0.0.1:51180 [500]: GET /send.php - Uncaught Error: Call to a member function push() on null in /home/ganbatte/Desktop/123/send.php:4
Why is that and how can I fix it?
Right after instanciating the object the $ws property does not have any value yet. Yet you try to access it. It looks like you have to start it first, like this:
include ('websocket.php');
$n = new websocket();
$n->start(); // <-- add this line
$n->ws->push(1, "asdf", 1, true);
However, given that there is a sendMessage() method as well, I guess you should probably use that, but I am not deep into swoole at all.
This looks like the docs you are looking for: Get Started with Swoole
And maybe it is a good idea to read up on the systematic basics there too.
Remember that this send method sends a message "on" the websocket to the attached clients, not from the clients to the server (a client, most likely a browser, has to do that part).
This code snippet explains sendMessage() and push to fds in scopes
`$server->fds = [];
$server->on('open', function (swoole_websocket_server $server, $request)
{
// add fd to scope
$server->fds[$request->fd] = true; // dummy-bool
});
$server->on('close', function ($server, $fd) {
// delete fd from scope
unset($server->fds[$fd]);
});
$server->on('message', function (swoole_websocket_server $server, $frame)
{
$message = "simple test message number: " . rand(1,10000);
// pipe the message to all 9 other workers
for ($i=0; $i < 10; $i++) { // WORKER_NUM
if ($i !== $server->worker_id)
// in this case only workers (no taskworkers)
$server->sendMessage($message, $i);
}
// message to all fds in this scope
testMessageSender($server, $message);
});
$server->on('pipeMessage', function($server, $src_worker_id, $data) {
// send to your known fds in worker scope
testMessageSender($server, $data);
});
function testMessageSender(&$server, $message){
// use only your own scope fds
foreach ($server->fds as $fd => $dummyBool) {
// push to your connected clients
$server->push($fd, $message);
}
}`
Reference:
Swoole Official Discussion

How to run a computation while running a websocket server in PHP?

I have the following scenario:
I have an API built with the Slim PHP framework. I am using the PHP lib Ratchet to run a WebSocket server. Once the WebSocket server is started, I want to run a function that does some computation while the server is running.
So far, inside my API, I have a route that calls the MyMethod method of a class MyClass. Inside the class, I have the following:
class MyClass {
public $calculation_status;
public function MyMethod() {
$server = IoServer::factory(
new HttpServer(
new WsServer(
new messengerApp($this)
)
),
8080
);
$this->doCalculationAsynchronously()->then(
function ($result) {
$this->calculation_status = 'finished';
},
function ($reason) {
$this->calculation_status = 'stopped';
},
function ($update) {
$this->calculation_status = 'still working...';
}
}
$server->run($this);
}
public function doCalculationAsynchronously() {
$deferred = new Deferred();
$this->computeSomethingAsync(function ($error = null, $result) use ($deferred) {
if ($error) {
$deferred->reject($error);
} else {
$deferred->resolve($result);
}
});
return $deferred->promise();
}
public function computeSomethingAsync() {
// Simulate a long running calculation
while(true){} // OR sleep(1000000);
return $result;
}
}
So, I'm expecting this to try to start running the asynchronous calculation function, return a promise to MyMethod, and then run my WebSocket server.
The reason for injecting $this into the server is to access my calculation_status property and be able to send it to clients through the WS.
This code is inspired by the example for Deferred in the ReactPHP doc
Note: If I don't have the forever while loop, it goes on and runs the server correctly (but this is synchronous behavior; my goal for the server is to send the calculation status to clients). Injecting the class into the object works fine as well.

Understanding retry logic in the AWS PHP SDK

It seems that only the DynamoDB and S3Clients have retry logic enabled.
It seems like the retries config value has no effect on other services. Is there an easy way to enable this on others (e.g. SQS), or have I misunderstood this functionality?
I've located the clientConfig.setUseThrottleRetries(true); option in the Java SDK, but have yet to find an equivalent in the PHP SDK.
You have misunderstood: SQS does have retries enabled.
To test: try the following:
$sqsClient = new sqqClient('2012-11-05', ['retries' => 2]);
$startTime = time();
try {
$sqsClient->receiveMessages(['WaitTimeSeconds' => 5]);
} catch(Exception $e) {
}
$timeTaken = time() - $startTime;
echo $timeTaken;
and do not send any messages. You'll see
10
as that is #WaitTime * #retries
If you get no messages, it's considered a failure and will retry to get some.
S3 and DynamoDB have special cases - which is what you noticed.
Edit:
This works: hacking AWS code.
class final class Middleware
{
public static function retry(
callable $decider = null,
callable $delay = null,
$stats = false
) {
echo 'Forcing retries to false';
$decider = function() {return false;};
...
This doesn't: in my code.
$decider = function() {
echo 'No retries';
return false;
};
$client->getHandlerList()->appendSign(\AWS\Middleware::retry($decider, null), 'retry');

voryx thruway multiple publish

I need to publish messages from php script, I can publish a single message fine. But now I need to publish different messages in loop, can't find proper way how to do it, here is what I tried:
$counter = 0;
$closure = function (\Thruway\ClientSession $session) use ($connection, &$counter) {
//$counter will be always 5
$session->publish('com.example.hello', ['Hello, world from PHP!!! '.$counter], [], ["acknowledge" => true])->then(
function () use ($connection) {
$connection->close(); //You must close the connection or this will hang
echo "Publish Acknowledged!\n";
},
function ($error) {
// publish failed
echo "Publish Error {$error}\n";
}
);
};
while($counter<5){
$connection->on('open', $closure);
$counter++;
}
$connection->open();
Here I want to publish $counter value to subscribers but the value is always 5, 1.Is there a way that I open connection before loop and then in loop I publish messages
2.How to access to $session->publish() from loop ?
Thanks!
There are a couple different ways to accomplish this. Most simply:
$client = new \Thruway\Peer\Client('realm1');
$client->setAttemptRetry(false);
$client->addTransportProvider(new \Thruway\Transport\PawlTransportProvider('ws://127.0.0.1:9090'));
$client->on('open', function (\Thruway\ClientSession $clientSession) {
for ($i = 0; $i < 5; $i++) {
$clientSession->publish('com.example.hello', ['Hello #' . $i]);
}
$clientSession->close();
});
$client->start();
There is nothing wrong with making many short connections to the router. If you are running in a daemon process though, it would probably make more sense to setup something that just uses the same client connection and then use the react loop to manage the loop instead of while(1):
$loop = \React\EventLoop\Factory::create();
$client = new \Thruway\Peer\Client('realm1', $loop);
$client->addTransportProvider(new \Thruway\Transport\PawlTransportProvider('ws://127.0.0.1:9090'));
$loop->addPeriodicTimer(0.5, function () use ($client) {
// The other stuff you want to do every half second goes here
$session = $client->getSession();
if ($session && ($session->getState() == \Thruway\ClientSession::STATE_UP)) {
$session->publish('com.example.hello', ['Hello again']);
}
});
$client->start();
Notice that the $loop is now being passed into the client constructor and also that I got rid of the line disabling automatic reconnect (so if there are network issues, your script will reconnect).

Return synchronously when a React/Promise is resolved

I need to return from a function call once a React/Promise has been resolved. The basic idea is to fake a synchronous call from an ansynchronous one. This means that the outer function must return a value once a promise has been resolved or rejected.
This is to create a driver for RedBeanPHP using React/Mysql. I am aware that this will likely lead to CPU starvation in the React event loop.
My initial idea was to use a generator then call yield inside a \React\Promise\Deferred::then callback.
function synchronous()
{
$result = asynchronous();
}
function asynchronous()
{
$deferred = new \React\Promise\Deferred;
$sleep = function() use ($deferred)
{
sleep(5);
$deferred->resolve(true);
};
$deferred->then(function($ret) {
yield $ret;
});
$sleep();
}
The PHP generator class, AFAICT, is only directly constructable by the PHP engine itself. The then callback would need to directly invoke send on the generator of the asynchronous function for this to work.
PHP lacks both continuations as well as generator delegation, which would make it possible to call yield from inside a nested callback, making this entirely impossible to achieve for the moment.
ReactPhp offers the async tools package which has an await function.
Code can then become:
function synchronous()
{
$result = \React\Async\await(asynchronous());
}
function asynchronous()
{
$deferred = new \React\Promise\Deferred;
$sleep = function() use ($deferred)
{
sleep(5);
$deferred->resolve(true);
};
$sleep();
return $deferred->promise();
}

Categories