Repository navigation
Description
Hello,
I ran into an interesting issue with this library when attempting to use Devristo/phpws and its builtin websocket client. When data is read from a stream using the Stream class' handleData function, reads seem to be blocking even though stream_set_blocking is called in __construct. I found that replacing line 121's fread with a stream_get_contents resolved my blocking reads issue. I feel like this class should be using the stream_ functions anyways - but it seems like a mix of f* and stream_ functions are used.
Is there a possibility a fix for this can be issued?
The only way I know to reproduce this involves a lot of steps and a specific product (Slack) and protocol (Hybi Websockets). I have tested that this issue exists on multiple boxes using PHP 5.5. I'll outline the steps to reproduce..
- Sign up for a free Slack team here: https://slack.com/
- Use the slack API doc site to 'test' the
rtm.startcall. This will give you a Websocket URL to connect to (must be used within 30 seconds, FYI). It's at the very bottom of the output. https://api.slack.com/methods/rtm.start/test - Connect using Devristo/phpws using the URL you got in step 2.
require_once("vendor/autoload.php"); // Composer autoloader
$loop = \React\EventLoop\Factory::create();
$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");
$logger->addWriter($writer);
$client = new \Devristo\Phpws\Client\WebSocket("wss_url_here", $loop, $logger);
$client->on("request", function($headers) use ($logger){
$logger->notice("Request object created!");
});
$client->on("handshake", function() use ($logger) {
$logger->notice("Handshake received!");
});
$client->on("connect", function($headers) use ($logger, $client){
$logger->notice("Connected!");
});
$client->on("message", function($message) use ($client, $logger){
$logger->notice("Got message: ".$message->getData());
});
$client->open();
$loop->run();- Type messages on the Slack web client from one or more users. Note that your PHP app will always be one event behind because it it blocking until the next read comes in.
Again, I know this is kind of a weird example, but it's the only thing I've been using this lib for and I don't have another websocket service I can test. It may be related to how the websocket clients end their frames/lines and how fread expects them to end... I'm not completely sure.
Let me know if you need more information on this. I will be happy to provide what I can.
Activity
slowbro commentedon Feb 2, 2015
FYI it looks like a similar issue was noted in reactphp/socket:
https://github.com/reactphp/socket/blob/master/src/Connection.php#L11-L13
In my fork, I have changed the
freadto astream_socket_revcfrombut unfortunately this caused the websocket connection to never complete. This specifically may be an issue with phpws, and not with stream.Edit: Looks like
stream_socket_revcfrombypasses TLS/SSL filters and is returning encrypted data (which explains that..), sostream_get_*may be the only option in this sense.cboden commentedon Feb 2, 2015
What operating system are you running this on?
slowbro commentedon Feb 2, 2015
This is on CentOS. I have tested it on 6.5 and 6.6.
cboden commentedon Feb 2, 2015
First try taking your logger out. By default the STDOUT stream blocks. If your'e still having the issue next I'd suggest trying Pawl as your WebSocket client.
slowbro commentedon Feb 2, 2015
I tried setting the logger to /dev/null instead of php://stdout, but I got the same result. Unfortunately phpws requires the logger. I'm pretty positive that it's not related to the logger.. the
freadin Stream was what was hanging and confirmed by some crude debuggingechos before and after it. Could STDOUT still cause thatfreadto block..?In any case, Pawl looks nice.. I may end up going that route.
slowbro commentedon Feb 2, 2015
Okay, I tested this with Pawl as well, completely removing my code and the logger except for a required API call to get the websocket URL. It still lags behind by one message..
As of the above, I had sent two messages: "test" and "test2". When using
stream_get_contentsthese app appear instantly. When usingfread, they do not show up until the next event JSON comes in from Slack. For example, 'test1' only came in because I started typing 'test2' and auser_typingevent came in. After I had submitted 'test', the output was stopped at the firstuser_typingand would not move forward until I began typing again.I hope that all makes sense.
slowbro commentedon Feb 2, 2015
I took it even further and took Yii out of the loop as well. Just a bare test program using only Pawl and a url provided from argv..
This, still, showed the same symptoms as phpws. If the
freadis changed to astream_get_contents, it works as expected - otherwise it blocks.cboden commentedon Feb 2, 2015
What's the output of
composer show -i?slowbro commentedon Feb 2, 2015
This is in a brand new folder - only Pawl in composer.json.
cboden commentedon Feb 2, 2015
Hmm, I was thinking it could have been an SSL bug we recently (thought) we fixed in socket-client...Can you try the same thing but without SSL by any chance?
slowbro commentedon Feb 2, 2015
Unfortunately I can't get a ws:// for Slack.. they only give me wss :(
I tested with ws(s)://echo.websocket.org and it seems to work OK.. I
$conn->send("hello!!");before$conn->on('message' ...).I don't know of anything else to test with; perhaps something more involved than an echo server. Do you have any ideas?
slowbro commentedon Feb 2, 2015
For fun, I tested the Slack WSS URL with an external tester. I used the "Simple WebSocket Client" Chrome Extension, connected to the URL that Slack gave me, and did not observe this problem. There was no blocking, and messages came through A-OK.
cboden commentedon Feb 3, 2015
In your vendor folder find this file/line - change the code to force
wrapSecurebe true. Does that solve the issue?slowbro commentedon Feb 3, 2015
Yep, that fixed the problem.
Perhaps it's an issue with Remi's 5.5.20 build - or it affects this version too?
cboden commentedon Feb 3, 2015
According to the latest comment the fix @DaveRandom applied has been reverted due to a bug that blocks connecting.
Until that's resolved (again) we'll have to remove that version check condition in SocketClient. I'll start work on that tonight after work.
slowbro commentedon Feb 3, 2015
OK - thank you! I really appreciate the help. It's a shame they reverted it :(
Would you like me to open up an issue on socket-client and reference/close this thread?
cboden commentedon Feb 3, 2015
Yes please!
slowbro commentedon Feb 3, 2015
Done! Thanks again!