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

Commit f989bb4

Browse files
committed
add demo
1 parent 4f0816c commit f989bb4

17 files changed

+789
-351
lines changed

.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
APP_NAME=Laravel
1+
APP_NAME="Laravel WebSockets"
22
APP_ENV=local
33
APP_KEY=
44
APP_DEBUG=true

app/Events/MessageSent.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\User;
6+
use App\Message;
7+
use Illuminate\Broadcasting\Channel;
8+
use Illuminate\Queue\SerializesModels;
9+
use Illuminate\Broadcasting\PrivateChannel;
10+
use Illuminate\Broadcasting\PresenceChannel;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Broadcasting\InteractsWithSockets;
13+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
14+
15+
class MessageSent implements ShouldBroadcast
16+
{
17+
use Dispatchable, InteractsWithSockets, SerializesModels;
18+
19+
/**
20+
* User that sent the message
21+
*
22+
* @var User
23+
*/
24+
public $user;
25+
26+
/**
27+
* Message details
28+
*
29+
* @var Message
30+
*/
31+
public $message;
32+
33+
/**
34+
* Create a new event instance.
35+
*
36+
* @return void
37+
*/
38+
public function __construct(User $user, Message $message)
39+
{
40+
$this->user = $user;
41+
$this->message = $message;
42+
}
43+
44+
/**
45+
* Get the channels the event should broadcast on.
46+
*
47+
* @return Channel|array
48+
*/
49+
public function broadcastOn()
50+
{
51+
return new PresenceChannel('chat');
52+
}
53+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Events\MessageSent;
6+
use App\Message;
7+
use Illuminate\Http\Request;
8+
9+
class ChatsController extends Controller
10+
{
11+
public function __construct()
12+
{
13+
$this->middleware('auth');
14+
}
15+
16+
public function index()
17+
{
18+
return view('chat');
19+
}
20+
21+
public function fetchMessages()
22+
{
23+
return Message::with('user')->get();
24+
}
25+
26+
public function sendMessage(Request $request)
27+
{
28+
$message = auth()->user()->messages()->create([
29+
'message' => $request->message
30+
]);
31+
32+
broadcast(new MessageSent(auth()->user(), $message))->toOthers();
33+
34+
return ['status' => 'Message Sent!'];
35+
}
36+
}

composer.lock

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
*/
172172
App\Providers\AppServiceProvider::class,
173173
App\Providers\AuthServiceProvider::class,
174-
// App\Providers\BroadcastServiceProvider::class,
174+
App\Providers\BroadcastServiceProvider::class,
175175
App\Providers\EventServiceProvider::class,
176176
App\Providers\RouteServiceProvider::class,
177177

config/broadcasting.php

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
'options' => [
3939
'cluster' => env('PUSHER_APP_CLUSTER'),
4040
'encrypted' => true,
41+
'host' => '127.0.0.1',
42+
'port' => 6001,
43+
'scheme' => 'http'
4144
],
4245
],
4346

public/css/app.css

+37
Original file line numberDiff line numberDiff line change
@@ -10472,3 +10472,40 @@ a.text-dark:focus {
1047210472
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
1047310473
}
1047410474

10475+
.chat {
10476+
list-style: none;
10477+
margin: 0;
10478+
padding: 0;
10479+
}
10480+
10481+
.chat li {
10482+
margin-bottom: 10px;
10483+
padding-bottom: 5px;
10484+
border-bottom: 1px dotted #B3A9A9;
10485+
}
10486+
10487+
.chat li .chat-body p {
10488+
margin: 0;
10489+
color: #777777;
10490+
}
10491+
10492+
.chats .card-body {
10493+
overflow-y: scroll;
10494+
height: 80vh;
10495+
}
10496+
10497+
::-webkit-scrollbar-track {
10498+
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
10499+
background-color: #F5F5F5;
10500+
}
10501+
10502+
::-webkit-scrollbar {
10503+
width: 12px;
10504+
background-color: #F5F5F5;
10505+
}
10506+
10507+
::-webkit-scrollbar-thumb {
10508+
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
10509+
background-color: #555;
10510+
}
10511+

0 commit comments

Comments
 (0)