Hi.
I've been playing with Ratched for a few days and I really like it so far. I have a question I haven't been able to figure out. Can you define regular http:// endpoints in the app? I didn't find this in the documentation or on the internet in general.
I managed to do kind of a workaround by defining a route to a custom controller
$app->route('/path', new \HttpController(), ['*']);
and then in the controller define the onOpen method like this
public function onOpen($conn, $request) {
$response = ['HTTP/1.1 200', '', 'example body'];
$conn->send(implode("\r\n", $response));
$conn->close();
}
but this only supports GET endpoints and seems like more of a hack of an existing mechanic. I was wondering if there is already existing wayof creating endpoints that I'm just missing
thanks
This is how you would go about doing it within Ratchet and yeah, it is a bit of a hack. This Ratchet library wasn't meant to be used as an HTTP server but a standalone WebSocket server. The idea at the time was that existing PHP websites would be served through FCGI and a sibling WebSocket app would run beside it.
If you'd like to run a full blown Reactive PHP server with both HTTP and WebSockets check out React/Http with WebSocketMiddleware.
Right, the idea I was going for was to have a socket server that could also have a few endpoints you could call and these endpoints would have some logic that would have access to all the connections and could potentially send message to one or more connections.
thanx for responding
That should be do-able. Because WebSockets are a protocol on top of HTTP Ratchet inherently is an HTTP server, the API just sort of obfuscates it. If you want to support POST you'd have to not use the App class as it hard codes GET.
Keep in mind that having ws and http endpoint on single threaded server, might lead to block each other ie. handling http request will "pause" streaming data over ws.