Код: Выделить всё
public function userChat(Request $request)
{
if (Auth::guard('admin')->check()) {
$user = Auth::guard('admin')->user()->name;
}
if (Auth::guard('web')->check()) {
$user = Auth::user()->name;
$userName = Auth::user()->name;
}
$user_id = $request->get('user_id');
$purpose = $request->get('purpose');
$owner = '';
if ($user == $user_id)
$owner = 'me';
$owner = 'you';
// dd($adminName);
if ($purpose == 'find-user') {
$msg = Chat::where('from', $user)->where('to', $user_id)
->orWhere('from', $user_id)->Where('to', $user)
->get();
// dd($msg);
return response()->json($msg);
} elseif ($purpose == 'admin-user') {
$val = $request->get('val');
$chat = Chat::create([
'message' => $val,
'from' => $user,
'to' => $user_id
]);
// Get Receiver MSG ID
$idReceiver = User::where('name', '=', $user_id)->pluck('id')->first();
$data =[
'sender' => $user,
'receiver' => $idReceiver,
];
event(new ChatNotify($data));
return response()->json(['user' => $user, 'idReceiver' => $idReceiver, 'msg' => $val]);
}
}
Код: Выделить всё
Pusher.logToConsole = true;
var pusher = new Pusher(MY_KEY, {
cluster: 'mt1',
encrypted: false
});
Код: Выделить всё
var notificationsWrapper = $('footer#footer_layouts');
// Subscribe to the channel we specified in our Laravel Event
var channel = pusher.subscribe('chat-notify');
// Bind a function to a Event (the full Laravel class)
channel.bind('App\\Events\\ChatNotify', function (data) {
alert('hi');
var newNotificationHTML = `
New Message From ` + data.sender + `
`;
notificationsWrapper.append(newNotificationHTML);
});
Код: Выделить всё
public $sender;
public $receiver;
public function __construct($data)
{
$this->sender = $data['sender'];
$this->receiver = $data['receiver'];
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['chat-notify'];
}
Код: Выделить всё
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=1344521
PUSHER_APP_KEY=XXXXXXX
PUSHER_APP_SECRET=XXXXXXX
PUSHER_APP_CLUSTER=mt1
Код: Выделить всё
App\Providers\BroadcastServiceProvider::class,
Код: Выделить всё
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
//'useTLS' => true,
],
],
Подробнее здесь: https://stackoverflow.com/questions/645 ... -broadcast