-
Notifications
You must be signed in to change notification settings - Fork 1
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
Websockets support #121
base: master
Are you sure you want to change the base?
Websockets support #121
Conversation
Here's the test: diff --git a/src/it/php/web/unittest/IntegrationTest.class.php b/src/it/php/web/unittest/IntegrationTest.class.php
index a546932..678dca0 100755
--- a/src/it/php/web/unittest/IntegrationTest.class.php
+++ b/src/it/php/web/unittest/IntegrationTest.class.php
@@ -1,6 +1,7 @@
<?php namespace web\unittest;
use test\{Assert, After, Test, Values};
+use websocket\WebSocket;
#[StartServer(TestingServer::class)]
class IntegrationTest {
@@ -161,4 +162,17 @@ class IntegrationTest {
$r= $this->send('GET', '/cookie', '1.0', ['Cookie' => $header]);
Assert::equals((string)strlen($header), $r['body']);
}
+
+ #[Test]
+ public function websocket_message() {
+ try {
+ $ws= new WebSocket($this->server->connection, '/ws');
+ $ws->connect();
+ $ws->send('Test');
+ $echo= $ws->receive();
+ } finally {
+ $ws->close();
+ }
+ Assert::equals('Echo: Test', $echo);
+ }
} This institutes a dependency on the websocket client implementation - xp-forge/websockets#6 - and the ability to pass paths - xp-forge/websockets#7 - and would thus bump the minimum version requirement for the respective library to |
This also removes support for PHP versions < 7.4 as the websockets library requires 7.4+ in its 4.0-SERIES. See xp-framework/rfc#343 and #121 (comment)
This could be done by starting the development webserver as a backend and then:
A POC shows this is viable. |
…o it To start, this is a simple reverse proxy setup, which is exactly what happens for HTTP request. Websockets, on the other hand, are kept alive in this server, and its messages are translated to server-sent events, before being passed to the backend, which doesn't support this protocol. This enables the highly effective "save-rerun" developer experience with no intermediary steps like restarting the server (and potentially losing state), or compiling (even with the XP Compiler in place, this will happen "just in time"). We could have chosen any wire format to send the messages back and forth but chose to go with something well-established and standardized, in this case opting for https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events
// \util\cmd\Console::writeLine('>>> ', $message); | ||
$this->backend->write($message."\r\n"); | ||
foreach ($this->transmit($request->incoming(), $this->backend) as $step) { | ||
// yield 'read' => $socket; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is commented to guarantee synchronous execution of this method due to the backend server only being able to handle one request at a time. We could consider using a worker pool in order to speed things up in a future pull request, or handling requests to static resources inside the proxy itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple smaller things to be addressed
Consistent with naming in util.URI class, see xp-forge/uri#10
This pull request adds websockets to the web server.
In a nutshell
Complete implemenation at https://gist.github.com/thekid/f933e8ffbc59b35af5e639bdd6f538c2
Backwards compatibility
This pull request breaks BC and thus institutes a 5.0-RELEASE, with the necessity to create compatibility releases for these libraries:
All libraries except lambda-ws continue to work as intended. For the AWS Lambda web adapter, see xp-forge/lambda-ws#13
TODO
http
andwebsocket
See also