Giter Site home page Giter Site logo

laravel-broadcasting's Introduction

Real-time Event Broadcasting Setup (Laravel + ReactJS + InertiaJS + PusherJS)

Laravel Setup

  1. Create an Event:

    php artisan make:event DocumentEvent
    
    
  2. Create Listener

    php artisan make:listener DocumentListener --event=DocumentEvent
    
  3. inside DocumentEvent

    class DocumentEvent implements ShouldQueue ,ShouldBroadcast
    {
     use Dispatchable, InteractsWithSockets, SerializesModels;
    
    
    
     /**
      * Create a new event instance.
      */
     public function __construct(public $count)
     {
     }
    
     /**
      * Get the channels the event should broadcast on.
      *
      * @return array<int, \Illuminate\Broadcasting\Channel>
      */
     public function broadcastOn(): Channel
     {
         return new Channel('notification');
     }
     }
    
  4. Inside Listener

    class DocumentListener implements ShouldQueue, ShouldHandleEventsAfterCommit
    {
    
    
     use InteractsWithQueue;
    
    
     
     public $queue = "high";
     /**
      * Create the event listener.
      */
     public function __construct()
     {
         //
     }
    
     /**
      * Handle the event.
      */
     public function handle(DocumentEvent $event): void
     {
         for ($i=0; $i < $event->count; $i++) { 
             User::create([
                 'name' => fake()->name(),
                 'email' => fake()->unique()->safeEmail(),
                 'email_verified_at' => now(),
                 'password' => Hash::make("password")
             ]);
         }
     }
    
    
     public function failed(DocumentEvent $event , Throwable $th)
     {
         report($th);
     }
    
     public function retryUntill(): DateTime
     {
         return now()->addMinute(3);
     }
     }
    
  5. Register Event and Listener inside EventServiceProvider

     protected $listen = [
         Registered::class => [
             SendEmailVerificationNotification::class,
         ],
    
         DocumentEvent::class => [
             DocumentListener::class
         ],
     ];
    
  6. set up Channel Route inside Channel Route in Laravel router folder

    Broadcast::channel('notification', function () {
    return true; // Define your authentication logic here
    });
  7. in bootstrap.js, we export WebsocketEcho for reusability in other Reactjs pages you want realtime Environment

    • make sure each env is configured successfull in .env file for each key
    window.Pusher = Pusher;
    export const WebSocketEcho = new Echo({
     broadcaster: 'pusher',
     key: import.meta.env.VITE_PUSHER_APP_KEY,
     cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1',
     wsHost: import.meta.env.VITE_PUSHER_HOST ? import.meta.env.VITE_PUSHER_HOST : `ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`,
     wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
     wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
     forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
     enabledTransports: ['ws', 'wss'],
     }); 
  8. Now , feel free to design any realtime env , mine was so simple overview

    • when user click a link , application generate number of user into db , and this scenario is queued in queed Jobs ,
    • App through websocket send notification to browser without any page refresh from user
    • this happen automatically and number of users from db is calculated by aggregate function from sql query
    • Good , now you understand the scenario , feel free to Think big on any realtime situation

    Handle in this way in Javascript file

     useEffect(()=> {
        const NotificationChannel = WebSocketEcho.channel("notification");
        NotificationChannel.listen("DocumentEvent", (event)=> {
            console.log(event);
        })
        return ()=> {
            NotificationChannel.stopListening("DocumentEvent");
        }
    
    }, []);

Job batching is Awesome !

public function Store(Request $request)
    {
        // $file = $request->file("filename");
        // $filepath = $file->storeAs("temp",'users.csv');
        //  dispatch(new ImportCsvFile($filepath));

        // dispatch Batches of Jobs
        $batch = Bus::batch([
            new ImportCsvFileWithBaches(1, 5),
            new ImportCsvFileWithBaches(6, 7),
            new ImportCsvFileWithBaches(7, 9),
            new ImportCsvFileWithBaches(9, 14),
            new ImportCsvFileWithBaches(15, 16),
        ])
            ->then(function (Batch $batch) {
                echo "JOB COMLETE :" . $batch->progress() . "\n\n";
                // [email protected]
                JobAnalysisEvent::dispatch($batch->progress(),
                $batch->totalJobs,
                "success",
                "Data successfull saved to database",
                true
            );
                
            })
            ->catch(function (Batch $batch, Throwable $e) {
                // First Job failure detected ...
                JobAnalysisEvent::dispatch($batch->progress(),
                $batch->totalJobs,
                "danger",
                "Something went wrong , some data not saved!",
                true
            );
                
            })
            ->finally(function (Batch $batch) {
                // The batch has finnished executing ...
            })
            ->onQueue("importcsv")
            // ->name("ImportCsv")
            ->dispatch();
        // dd($batch);

        return back();
    }

Another best way to import Larage data in term of chunk from collection

try {
            $file = $request->file('filename');
            $filepath = $file->storeAs("temp", 'userfile.csv');
            $fullpath = storage_path('app/' . $filepath);
            $rows = SimpleExcelReader::create($fullpath)
                ->getRows();
            $userChunks = $rows->chunk(30);
            foreach ($userChunks as $chunk) {
                ImportLargeCsvFileWithBatch::dispatch($chunk->toArray())
                      ->onQueue("importcsv");
            }
            return back();

        } catch (\Throwable $th) {
            dd($th);
        }

Thanks for reading

regard mrwilbroad

laravel-broadcasting's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.