Skip to content

Commit

Permalink
Refactored ping on new conection
Browse files Browse the repository at this point in the history
  • Loading branch information
slavarazum committed Dec 12, 2023
1 parent b1fbe82 commit f6f58d3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
1 change: 1 addition & 0 deletions routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Route::group([
'middleware' => [config('wave.auth_middleware', 'auth').':'.config('wave.guard')],
], function () {
Route::get('presence-channel-users', [PresenceChannelUsersController::class, 'index'])->name('presence-channel-users');
Route::post('presence-channel-users', [PresenceChannelUsersController::class, 'store'])->name('presence-channel-users');
Route::delete('presence-channel-users', [PresenceChannelUsersController::class, 'destroy']);

Expand Down
14 changes: 13 additions & 1 deletion src/Http/Controllers/PresenceChannelUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,29 @@ public function __construct(protected PresenceChannelUsersRepository $repository
$this->middleware((function ($request, $next) {
$this->userInfo = json_decode(Broadcast::auth($request), true, 512, JSON_THROW_ON_ERROR)['channel_data']['user_info'];

if ($request->has('socket_id')) {
$request->headers->set('X-Socket-Id', $request->socket_id);
}

return $next($request);
}));
}

public function index()
{
return response()->json($this->repository->getUsers(request()->channel_name));
}

public function store(Request $request)
{
if ($this->repository->join($request->channel_name, $request->user(), $this->userInfo, Broadcast::socket($request))) {
broadcast(new PresenceChannelJoinEvent($this->userKey($request->user()), $this->userInfo, Str::after($request->channel_name, 'presence-')))->toOthers();
}

return response()->json($this->repository->getUsers($request->channel_name));
return response()->json([
'_token' => csrf_token(),
'users' => $this->repository->getUsers($request->channel_name),
]);
}

public function destroy(Request $request)
Expand Down
18 changes: 2 additions & 16 deletions src/Http/Controllers/WaveConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,20 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Qruto\Wave\Events\SsePingEvent;
use Qruto\Wave\Http\Middleware\PingConnections;
use Qruto\Wave\Sse\ServerSentEventStream;
use Qruto\Wave\Storage\BroadcastEventHistory;

class WaveConnection extends Controller
{
public function __construct(protected BroadcastEventHistory $eventHistory)
{
$this->middleware(PingConnections::class);
}

public function __invoke(Request $request, ServerSentEventStream $responseFactory)
{
if ($this->shouldSendPing()) {
event(new SsePingEvent());
}

return $responseFactory->toResponse($request);
}

protected function shouldSendPing(): bool
{
if (! config('wave.ping.enable', true)) {
return false;
}

if (app()->environment(config('wave.ping.eager_env', 'local'))) {
return true;
}

return now()->getTimestamp() - $this->eventHistory->lastEventTimestamp() > config('wave.ping.frequency', 30);
}
}
42 changes: 42 additions & 0 deletions src/Http/Middleware/PingConnections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Qruto\Wave\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Qruto\Wave\Events\SsePingEvent;
use Qruto\Wave\Storage\BroadcastEventHistory;
use Symfony\Component\HttpFoundation\Response;

class PingConnections
{
public function __construct(protected BroadcastEventHistory $eventHistory)
{
}
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if ($this->shouldSendPing()) {
event(new SsePingEvent());
}

return $next($request);
}

protected function shouldSendPing(): bool
{
if (! config('wave.ping.enable', true)) {
return false;
}

if (app()->environment(config('wave.ping.eager_env', 'local'))) {
return true;
}

return now()->getTimestamp() - $this->eventHistory->lastEventTimestamp() > config('wave.ping.frequency', 30);
}
}
6 changes: 3 additions & 3 deletions src/Sse/ServerSentEventStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public function toResponse($request)

$newSocket = $this->generateConnectionId();

$request->headers->set('X-Socket-ID', $newSocket);
$request->headers->set('X-Socket-Id', $newSocket);

return $this->responseFactory->stream(function () use ($request, $lastSocket, $newSocket) {
if ($request->hasHeader('Last-Event-ID')) {
$missedEvents = $this->eventsHistory->getEventsFrom($request->header('Last-Event-ID'));
if ($request->hasHeader('Last-Event-Id')) {
$missedEvents = $this->eventsHistory->getEventsFrom($request->header('Last-Event-Id'));

$missedEvents
// TODO: except system channel
Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(public ?Authenticatable $user, public ?string $lastE
$test = $test->actingAs($user);
}

$this->response = $test->get(route('wave.connection'), $this->lastEventId ? ['Last-Event-ID' => $this->lastEventId] : []);
$this->response = $test->get(route('wave.connection'), $this->lastEventId ? ['Last-Event-Id' => $this->lastEventId] : []);
}

public function id()
Expand Down

0 comments on commit f6f58d3

Please sign in to comment.