Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 1dcf853

Browse files
authored
Merge pull request #524 from beyondcode/feature/healthchecks
[feature] Healthcheck endpoint
2 parents 86fbf76 + 1ec637f commit 1dcf853

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

config/websockets.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@
267267

268268
'websocket' => \BeyondCode\LaravelWebSockets\Server\WebSocketHandler::class,
269269

270+
'health' => \BeyondCode\LaravelWebSockets\Server\HealthHandler::class,
271+
270272
'trigger_event' => \BeyondCode\LaravelWebSockets\API\TriggerEvent::class,
271273

272274
'fetch_channels' => \BeyondCode\LaravelWebSockets\API\FetchChannels::class,

src/Server/HealthHandler.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Server;
4+
5+
use Exception;
6+
use GuzzleHttp\Psr7\Response;
7+
use Psr\Http\Message\RequestInterface;
8+
use Ratchet\ConnectionInterface;
9+
use Ratchet\RFC6455\Messaging\MessageInterface;
10+
use Ratchet\WebSocket\MessageComponentInterface;
11+
12+
class HealthHandler implements MessageComponentInterface
13+
{
14+
/**
15+
* Handle the socket opening.
16+
*
17+
* @param \Ratchet\ConnectionInterface $connection
18+
* @return void
19+
*/
20+
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
21+
{
22+
$response = new Response(
23+
200,
24+
['Content-Type' => 'application/json'],
25+
json_encode(['ok' => true])
26+
);
27+
28+
tap($connection)->send(\GuzzleHttp\Psr7\str($response))->close();
29+
}
30+
31+
/**
32+
* Handle the incoming message.
33+
*
34+
* @param \Ratchet\ConnectionInterface $connection
35+
* @param \Ratchet\RFC6455\Messaging\MessageInterface $message
36+
* @return void
37+
*/
38+
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
39+
{
40+
//
41+
}
42+
43+
/**
44+
* Handle the websocket close.
45+
*
46+
* @param \Ratchet\ConnectionInterface $connection
47+
* @return void
48+
*/
49+
public function onClose(ConnectionInterface $connection)
50+
{
51+
//
52+
}
53+
54+
/**
55+
* Handle the websocket errors.
56+
*
57+
* @param \Ratchet\ConnectionInterface $connection
58+
* @param WebSocketException $exception
59+
* @return void
60+
*/
61+
public function onError(ConnectionInterface $connection, Exception $exception)
62+
{
63+
//
64+
}
65+
}

src/Server/Router.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function routes()
4949
$this->get('/apps/{appId}/channels', config('websockets.handlers.fetch_channels'));
5050
$this->get('/apps/{appId}/channels/{channelName}', config('websockets.handlers.fetch_channel'));
5151
$this->get('/apps/{appId}/channels/{channelName}/users', config('websockets.handlers.fetch_users'));
52+
$this->get('/health', config('websockets.handlers.health'));
5253
}
5354

5455
/**

tests/HealthTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Test;
4+
5+
use BeyondCode\LaravelWebSockets\Server\HealthHandler;
6+
use Illuminate\Support\Str;
7+
8+
class HealthTest extends TestCase
9+
{
10+
public function test_health_handler()
11+
{
12+
$connection = $this->newConnection();
13+
14+
$this->pusherServer = app(HealthHandler::class);
15+
16+
$this->pusherServer->onOpen($connection);
17+
18+
$this->assertTrue(
19+
Str::contains($connection->sentRawData[0], '{"ok":true}')
20+
);
21+
}
22+
}

0 commit comments

Comments
 (0)