Coder Social home page Coder Social logo

Comments (10)

sangio90 avatar sangio90 commented on June 27, 2024

I went into more depth to find out that the problem was in the send/close method.

They are called synchronously, but the stream is passed to the server asynchronously and in my case they are all waiting for my procedure to complete (don't know why).

The only way around was to execute the rest of my procedure in a callback called on $conn event "close".

e.g.

//Step 1, 2, 3 of my procedures
connect('ws://localhost:12345')->then(function($conn) {
    $conn->send('step 1,2, 3 completed');
    $conn->close();
    
});

$conn->on('close', function() {
   //Step 4,5
});

Anyone of you found a better solution?

from pawl.

clue avatar clue commented on June 27, 2024

[…] a long procedure (30 to 60 seconds).

ReactPHP works under the assumption that the loop must not be blocked. If you block the loop for this period of time, you're going to run into problems, see also https://github.com/reactphp/react/wiki/FAQ.

This is a pretty common problem. As an alternative, I would suggest moving these kind of things off to a worker queue.

from pawl.

sangio90 avatar sangio90 commented on June 27, 2024

The strange thing here is that I was expecting the ->send function to block the rest of the execution, instead what I got was a super fast ->send and ->close call, (that did nothing) and then the rest of my steps (4,5 which where just after the ->close call), and only then the ->send call would be really executed. Is it right?

What I want to achieve is a batch script which informs the WS Server (and the clients) of its progression, so on each step I want to send a message to the server telling it "I've done step X".

from pawl.

clue avatar clue commented on June 27, 2024

Both send() and close() are async. And so should be your "long procedure". Maybe use react/child-process to spawn this in a separate process and use STDIN/STDOUT to send "progress" messages?

from pawl.

sangio90 avatar sangio90 commented on June 27, 2024

I'll try the child-process and let you know. My "progress" messages should be sent to the client which should reload a grid whenever some of the steps are completed, so I cannot use stdin/out. I'm not sure if there are best practices on how to handle these problems.

from pawl.

clue avatar clue commented on June 27, 2024

Here's some pseudo code to get you started 👍

$process = new Process('ping google.com');
$process->start($loop);
$process->stdout->on('data', function ($chunk) use ($conn) {
    $conn->send($chunk);
});

from pawl.

cboden avatar cboden commented on June 27, 2024

In addition to what Christian has said part of the reason nothing happens until the end of your procedure is that when using nonblocking I/O in PHP the code is lazy and doesn't execute until the event loop starts running, which if you're using the "simple" example from the README, happens at the end of the process.

from pawl.

sangio90 avatar sangio90 commented on June 27, 2024

So in the end I should split my procedure into multiple Commands, at the end of which I send a websocket message and call php-cli process to execute the next step, right?

from pawl.

clue avatar clue commented on June 27, 2024
$process = new Process('php procedure.php');
$process->start($loop);
$process->stdout->on('data', function ($chunk) use ($conn) {
    $conn->send($chunk);
});
# prodecure.php
<?php

echo "hello\n";

sleep(2);
echo "1\n";

sleep(10);
echo "2\n";

sleep(2);
echo "3\n";

sleep(2);
echo "done\n";

from pawl.

sangio90 avatar sangio90 commented on June 27, 2024

Thanks guys!

from pawl.

Related Issues (20)

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.