Skip to content

imteajsajid12/Laravel-websocket-live-chat

Repository files navigation

Build Status Total Downloads Latest Stable Version License

About Laravel

Installation

Install Pusher

composer require pusher/pusher-php-server "~4.0"

Change .env broadcust_drive

BROADCAST_DRIVER=pusher

Use Pusher key


PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

Install Echo

npm install --save-dev laravel-echo pusher-js

resources/js/bootstrap.js

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true
});

use npm run watch

creat event

php artisan make:event Notify

Event/Notify

<?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 Notify implements ShouldBroadcast
{
    public $message;
    use Dispatchable, InteractsWithSockets, SerializesModels;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('chatify');
    }
}

channles.php

Broadcast::channel('chat', function ($user) {
    return  $user;
});

config/app/provider

        App\Providers\BroadcastServiceProvider::class,

web.php

Route::get('/lesson', [App\Http\Controllers\HomeController::class, 'index1'])->name('lesson');
Route::get('/send', [App\Http\Controllers\HomeController::class, 'send'])->name('send');
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

homeController.php

<?php

namespace App\Http\Controllers;

use App\Events\Notify;
use Illuminate\Console\Scheduling\Event as SchedulingEvent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {

        return view('home');
    }
    public function send()
    {
        event(new Notify('hey how are You'));
        return back();
    }
    public function index1()
    {
        // event(new Notify('hey how are You'));
        return view('home2');
    }
}

run

php artisan serve