Skip to content

Commit

Permalink
feat : realtime with laravel websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
aronei44 committed Aug 14, 2022
1 parent 16a569b commit 20ba0e8
Show file tree
Hide file tree
Showing 12 changed files with 1,840 additions and 218 deletions.
3 changes: 0 additions & 3 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ example for get route list:
docker container exec php php artisan route:list
```

# Running websocket

```
docker container exec php php artisan websockets:serve
```

# ++ Feature

- Hot Reload NextJs. you dont need rebuild container in development
Expand All @@ -46,6 +52,8 @@ docker container exec php php artisan route:list

- For documentaion, open 127.0.0.1:8000/api/documentation or http://localhost:8000/api/documentation

- For realtime socket dashboard, open 127.0.0.1/laravel-websockets


# Contributing

Expand Down
44 changes: 44 additions & 0 deletions api/app/Events/NewMessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NewMessageEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*
* @return void
*/
protected $user;
public $id, $room;
public function __construct($user, $id, $room)
{
$this->user = $user;
$this->id = $id;
$this->room = $room;
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('Cetan-'.$this->user);
}
public function broadcastAs()
{
return 'NewMessage';
}
}
5 changes: 4 additions & 1 deletion api/app/Http/Controllers/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Room;
use App\Models\Message;
use Illuminate\Http\Request;
use App\Events\NewMessageEvent;
use App\Http\Resources\MessageResource;

class MessageController extends Controller
Expand All @@ -31,6 +32,8 @@ public function store(Request $request)
]);
$room->updated_at = now();
$room->save();
$op = $room->user_1_id == $request->user()->id ? $room->user_2_id : $room->user_1_id;
broadcast(new NewMessageEvent($op, $message->id, $room->id));
return (new MessageResource($message))
->response()
->setStatusCode(201);
Expand Down Expand Up @@ -67,7 +70,7 @@ public function getMessageById(Request $request, Message $message)
{
try {
$room = Room::find($message->room_id);
if(($room->user_1_id != $request->user()->id && $room->user_2_id != $request->user()->id) || $message->user_id == $request->user()->id)
if(($room->user_1_id != $request->user()->id && $room->user_2_id != $request->user()->id))
{
return response()->json([
'status'=>'No Content'
Expand Down
4 changes: 3 additions & 1 deletion api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"license": "MIT",
"require": {
"php": "^8.0.2",
"beyondcode/laravel-websockets": "^1.13",
"darkaonline/l5-swagger": "^8.3",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^2.15",
"laravel/tinker": "^2.7"
"laravel/tinker": "^2.7",
"pusher/pusher-php-server": "^7.0"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
Expand Down
Loading

0 comments on commit 20ba0e8

Please sign in to comment.