Comments (10)
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.
[…] 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.
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.
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.
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.
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.
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.
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.
$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.
Thanks guys!
from pawl.
Related Issues (20)
- Connection closed (4005 - Already authenticated.) HOT 1
- underlying connection closed 1006 HOT 1
- Allow guzzlehttp/psr7 v2 HOT 1
- Call to undefined function GuzzleHttp\Psr7\uri_for() HOT 3
- Call to undefined function GuzzleHttp\Psr7\uri_for() HOT 6
- How to send command line live output to websocket server?
- send variable HOT 2
- Implementation to listen for socket.io events
- Sending binary messages refuse to work with specific char suite. HOT 2
- Cannot get TLS to work on proxied websocket with nginx
- Question - RabbitMq consumer freezing the websocket HOT 4
- How to authenticate with PEM certificate? HOT 2
- send message from php to websocket HOT 1
- reconnect to server when internet connection has been intrupted
- Message lost on 2 separate connections to same URL HOT 1
- PHP Warning: stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE.
- Unhandled promise rejection with DomainException: HTTP/1.1 200 OK
- Uncaught UnderflowException: Frame
- conflict with laravel-websockets HOT 1
- Immediately send and submit to the server.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pawl.