Skip to content

Commit cfb5b5e

Browse files
committed
Broadcasting (base + sample usage)
1 parent 7b168cd commit cfb5b5e

33 files changed

Lines changed: 2139 additions & 19 deletions

File tree

.docker/compose.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ services:
107107
profiles:
108108
- dev
109109

110+
# Reverb WebSocket server
111+
reverb:
112+
build:
113+
context: ..
114+
dockerfile: .docker/php/Dockerfile
115+
args:
116+
PUID: ${PUID:-1000}
117+
PGID: ${PGID:-1000}
118+
restart: unless-stopped
119+
command: php artisan reverb:start --host=0.0.0.0 --port=8080
120+
ports:
121+
- "${DOCKER_REVERB_PORT:-8080}:8080"
122+
volumes:
123+
- ..:/var/www/html
124+
- ./php/php.ini:/usr/local/etc/php/conf.d/custom.ini
125+
environment:
126+
- DB_HOST=${DB_HOST:-mysql}
127+
- APP_ENV=${APP_ENV:-local}
128+
- REVERB_APP_ID=${REVERB_APP_ID}
129+
- REVERB_APP_KEY=${REVERB_APP_KEY}
130+
- REVERB_APP_SECRET=${REVERB_APP_SECRET}
131+
- REVERB_HOST=${REVERB_HOST}
132+
- REVERB_PORT=${REVERB_PORT}
133+
- REVERB_SCHEME=${REVERB_SCHEME}
134+
profiles:
135+
- dev
136+
- testing
137+
110138
# Playwright server for browser testing
111139
playwright:
112140
image: mcr.microsoft.com/playwright:v1.57.0-noble

app/Console/Commands/BenchmarkGlpkBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function initializeSolver(): void
3838

3939
$this->solver = app(GlpkSolver::class);
4040

41-
$this->manifest = new class () extends LotteryManifest {
41+
$this->manifest = new class() extends LotteryManifest {
4242
public function __construct()
4343
{
4444
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
/**
11+
* Base abstract class for real-time notification events.
12+
*
13+
* All events meant to be presented as user notifications must extend this class
14+
* and implement the message() method to provide user-facing text.
15+
*/
16+
abstract class RealTimeNotification implements ShouldBroadcast
17+
{
18+
use Dispatchable;
19+
use InteractsWithSockets;
20+
use SerializesModels;
21+
22+
/**
23+
* Get the user-facing notification message.
24+
* This will be shown in the notifications UI.
25+
*/
26+
abstract public function message(): string;
27+
28+
/**
29+
* Get the channels the event should broadcast on.
30+
*
31+
* @return array<int, \Illuminate\Broadcasting\Channel>
32+
*/
33+
abstract public function broadcastOn(): array;
34+
35+
/**
36+
* The event's broadcast name.
37+
*/
38+
abstract public function broadcastAs(): string;
39+
40+
/**
41+
* Get the data to broadcast.
42+
*/
43+
abstract public function broadcastWith(): array;
44+
}

app/Events/ResourceCreated.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
/**
9+
* Generic event for when any resource is created.
10+
*
11+
* This event broadcasts resource creation to the frontend,
12+
* providing a consistent interface for all model types.
13+
*/
14+
class ResourceCreated extends RealTimeNotification
15+
{
16+
/**
17+
* Create a new event instance.
18+
*/
19+
public function __construct(public Model $model)
20+
{
21+
// ...
22+
}
23+
24+
/**
25+
* Get the user-facing notification message.
26+
*/
27+
public function message(): string
28+
{
29+
$modelName = class_basename($this->model);
30+
31+
return __('events.resource_created', [
32+
'resource' => __("models.{$modelName}"),
33+
]);
34+
}
35+
36+
/**
37+
* Get the channels the event should broadcast on.
38+
*
39+
* @return array<int, \Illuminate\Broadcasting\Channel>
40+
*/
41+
public function broadcastOn(): array
42+
{
43+
$channelName = str(class_basename($this->model))->plural()->lower();
44+
45+
return [
46+
new Channel($channelName),
47+
];
48+
}
49+
50+
/**
51+
* The event's broadcast name.
52+
*/
53+
public function broadcastAs(): string
54+
{
55+
return 'ResourceCreated';
56+
}
57+
58+
/**
59+
* Get the data to broadcast.
60+
*/
61+
public function broadcastWith(): array
62+
{
63+
return [
64+
'base_class' => class_basename($this->model),
65+
'id' => $this->model->getKey(),
66+
'message' => $this->message(),
67+
// Note: Full model data removed for security.
68+
// Frontend should fetch the resource via API if needed.
69+
];
70+
}
71+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
// Copilot - Pending review
4+
// TEMPORARY TEST MIDDLEWARE - Remove after testing
5+
6+
namespace App\Http\Middleware;
7+
8+
use App\Services\Broadcast\DataObjects\Message;
9+
use App\Services\Broadcast\Enums\BroadcastMessage;
10+
use App\Services\BroadcastService;
11+
use Closure;
12+
use Illuminate\Http\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
/**
16+
* Test middleware to broadcast user navigation events.
17+
*
18+
* This is TEMPORARY and should be removed after testing.
19+
*/
20+
class BroadcastNavigationTest
21+
{
22+
public function __construct(
23+
protected BroadcastService $broadcast,
24+
) {
25+
}
26+
27+
/**
28+
* Handle an incoming request.
29+
*
30+
* @param Closure(Request): (Response) $next
31+
*/
32+
public function handle(Request $request, Closure $next): Response
33+
{
34+
$user = $request->user();
35+
36+
if ($user) {
37+
// Broadcast navigation to user's private channel
38+
$this->broadcast->toUser(
39+
$user->id,
40+
Message::make(
41+
BroadcastMessage::USER_NAVIGATION,
42+
[
43+
'url' => $request->fullUrl(),
44+
'path' => $request->path(),
45+
'method' => $request->method(),
46+
'user_id' => $user->id,
47+
'user_name' => $user->fullname,
48+
],
49+
),
50+
);
51+
52+
// If user has a current project, also broadcast to project channel
53+
$projectId = $request->route('project')?->id ?? session('current_project_id');
54+
if ($projectId) {
55+
$this->broadcast->toProject(
56+
$projectId,
57+
Message::make(
58+
BroadcastMessage::USER_NAVIGATION,
59+
[
60+
'url' => $request->fullUrl(),
61+
'path' => $request->path(),
62+
'method' => $request->method(),
63+
'user_id' => $user->id,
64+
'user_name' => $user->fullname,
65+
'project_id' => $projectId,
66+
],
67+
),
68+
);
69+
}
70+
}
71+
72+
return $next($request);
73+
}
74+
}

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ protected function auth(Request $request): ?array
6767
return [
6868
'user' => [
6969
...$user->toResource()->withoutAbilities()->resolve(),
70+
'projects' => Project::all(),
7071
'can' => $this->policies(),
71-
'projects' => Project::pluck('id'),
7272
],
7373

7474
'notifications' => [

app/Models/Notification.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Services\Notifications\NotificationCollection;
88
use Illuminate\Database\Eloquent\Builder;
99
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10+
use Illuminate\Support\Facades\Auth;
1011

1112
class Notification extends Model
1213
{
@@ -143,13 +144,26 @@ public function scopeForUser(Builder $query, User $user): void
143144
}
144145

145146
/**
146-
* Set default query order as most recent first.
147+
* Set default sorting and filtering.
147148
*/
148149
protected static function booted(): void
149150
{
151+
/**
152+
* Load Notifications in reverse order by default.
153+
*/
150154
static::addGlobalScope(
151155
'recentFirst',
152156
fn (Builder $query) => $query->orderBy('id', 'desc')
153157
);
158+
159+
/**
160+
* Only include Notifications created after the User was invited to MTAV.
161+
*/
162+
if (Auth::check()) {
163+
static::addGlobalScope(
164+
'sinceRegistration',
165+
fn (Builder $query) => $query->where('created_at', '>', Auth::user()->created_at)
166+
);
167+
}
154168
}
155169
}

app/Policies/NotificationPolicy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function viewAny(User $user): bool
1919
*/
2020
public function view(User $user, Notification $notification): bool
2121
{
22-
return match($notification->target) {
22+
return match ($notification->target) {
2323
/** Private User channel: only available to the receipient */
2424
NotificationTarget::PRIVATE => $notification->target_id === $user->id,
2525

@@ -42,23 +42,23 @@ public function create(User $user): bool
4242
/**
4343
* Notifications cannot be updated.
4444
*/
45-
public function update(User $user, Notification $notification): bool
45+
public function update(User $user): bool
4646
{
4747
return false;
4848
}
4949

5050
/**
5151
* Notifications cannot be deleted manually.
5252
*/
53-
public function delete(User $user, Notification $notification): bool
53+
public function delete(User $user): bool
5454
{
5555
return false;
5656
}
5757

5858
/**
5959
* Notifications cannot be restored.
6060
*/
61-
public function restore(User $user, Notification $notification): bool
61+
public function restore(User $user): bool
6262
{
6363
return false;
6464
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
// Copilot - Pending review
4+
5+
namespace App\Services\Broadcast\DataObjects;
6+
7+
use App\Services\Broadcast\Enums\BroadcastMessage;
8+
9+
/**
10+
* Data object representing a broadcastable message.
11+
*
12+
* Encapsulates the message type and payload for broadcasting.
13+
*/
14+
class Message
15+
{
16+
/**
17+
* @param BroadcastMessage $type The type of message being broadcast
18+
* @param array<string, mixed> $data The data payload to send with the message
19+
* @param array<string, mixed> $metadata Optional metadata about the message
20+
*/
21+
public function __construct(
22+
public readonly BroadcastMessage $type,
23+
public readonly array $data = [],
24+
public readonly array $metadata = [],
25+
) {
26+
}
27+
28+
/**
29+
* Create a new message instance.
30+
*/
31+
public static function make(
32+
BroadcastMessage $type,
33+
array $data = [],
34+
array $metadata = [],
35+
): self {
36+
return new self($type, $data, $metadata);
37+
}
38+
39+
/**
40+
* Get the full payload to broadcast.
41+
*/
42+
public function toArray(): array
43+
{
44+
return [
45+
'type' => $this->type->value,
46+
'data' => $this->data,
47+
'metadata' => array_merge([
48+
'timestamp' => now()->toIso8601String(),
49+
], $this->metadata),
50+
];
51+
}
52+
53+
/**
54+
* Add metadata to the message.
55+
*/
56+
public function withMetadata(array $metadata): self
57+
{
58+
return new self(
59+
$this->type,
60+
$this->data,
61+
array_merge($this->metadata, $metadata),
62+
);
63+
}
64+
65+
/**
66+
* Add data to the message.
67+
*/
68+
public function withData(array $data): self
69+
{
70+
return new self(
71+
$this->type,
72+
array_merge($this->data, $data),
73+
$this->metadata,
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)