Running websockets on production without healthchecks is awful. AWS Elastic Beanstalk lets me use 6001 as HTTP port (it an ALB, so it supports Websockets) and it works, but my healthchecks fail because the port 6001聽is not a webserver and i literally can't do anything.
Is there any way of implementing a healthcheck function on this package?
https://github.com/beyondcode/laravel-websockets/pull/150 You can make a custom route to act like a health check endpoint.
After a long while, I came back using Websockets. If anyone wonders how I added a healthcheck on a 6001 route, here it is:
<?php
namespace App\WebSocketHandlers;
use Exception;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServerInterface;
class HealthSocketHandler implements HttpServerInterface
{
public function onMessage(ConnectionInterface $from, $msg)
{
//
}
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
{
$response = new Response(200, [
'Content-Type' => 'application/json',
], json_encode([
'ok' => true,
]));
$connection->send(\GuzzleHttp\Psr7\str($response));
$connection->close();
}
public function onClose(ConnectionInterface $connection)
{
//
}
public function onError(ConnectionInterface $connection, Exception $exception)
{
if (! $exception instanceof HttpException) {
return;
}
$response = new Response($exception->getStatusCode(), [
'Content-Type' => 'application/json',
], json_encode([
'error' => $exception->getMessage(),
]));
$connection->send(\GuzzleHttp\Psr7\str($response));
$connection->close();
}
}
In AppServiceProvider.php:
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
public function boot()
{
WebSocketsRouter::get('/health', HealthSocketHandler::class);
}
Opening up http://localhost:6001/health will show this with 200 OK as status:
{
"ok": true
}
Most helpful comment
After a long while, I came back using Websockets. If anyone wonders how I added a healthcheck on a 6001 route, here it is:
In
AppServiceProvider.php:Opening up http://localhost:6001/health will show this with
200 OKas status: