Laravel-websockets: Opening a webserver on 6001 for healthchecks

Created on 14 Aug 2019  路  2Comments  路  Source: beyondcode/laravel-websockets

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?

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:

<?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
}

All 2 comments

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
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmed-aliraqi picture ahmed-aliraqi  路  4Comments

sanjarani picture sanjarani  路  5Comments

ElegantSoft picture ElegantSoft  路  3Comments

vesper8 picture vesper8  路  3Comments

myckhel picture myckhel  路  3Comments