Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why is connect function always executed as last instruction? #69

Closed
sangio90 opened this issue Jan 19, 2018 · 10 comments
Closed

why is connect function always executed as last instruction? #69

sangio90 opened this issue Jan 19, 2018 · 10 comments

Comments

@sangio90
Copy link

Hello,
I'm trying to send multiple WS messages during a long procedure (30 to 60 seconds).
Calls to connect function were put in different places but what I notice is that they are all executed as last instructions of my procedure, so what I get is a bunch of messages sent at the end of the procedure.

Is it supposed to work this way? Should I use Threads to avoid it?

Thanks!

@sangio90
Copy link
Author

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?

@clue
Copy link
Member

clue commented Jan 19, 2018

[…] 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.

@sangio90
Copy link
Author

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".

@clue
Copy link
Member

clue commented Jan 19, 2018

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?

@sangio90
Copy link
Author

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.

@clue
Copy link
Member

clue commented Jan 19, 2018

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);
});

@cboden
Copy link
Member

cboden commented Jan 19, 2018

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.

@sangio90
Copy link
Author

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?

@clue
Copy link
Member

clue commented Jan 19, 2018

$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";

@sangio90
Copy link
Author

Thanks guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants