0

The Laravel SSE(server sent event) is a great solution to push the changes to frontend, however, on the server side, we need to have an efficient way to keep track of the updated record(s) before sending notification to frontend. however, the SSE requires a controller to work with, problem is how can the controller capture the Laravel events?

class HomeController extends Controller
{
    public function sse(SSE $sse)
    {
        // how to add the Laravel event listener here?
        return $sse->createResponse();
    }
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
eversun
  • 53
  • 7
  • 1
    why you need to do that in controller ? why not writing listener ? – Arsii Rasheed Oct 25 '19 at 19:15
  • suppose there's an API provided by server for client side able to receive the response with updated values, you will need to an URL for frontend able to get response, how to return the value to the frontend without using the controller to return the value to frontend? – eversun Oct 26 '19 at 21:11
  • you can use websockets or long pooling and broadcast channels to achieve that. (https://laravel.com/docs/6.x/broadcasting) – Arsii Rasheed Oct 27 '19 at 10:31

1 Answers1

0

Laravel provides with events, listeners, broadcasting and channels to communicate with front end via events. You don't need to do that in controllers. You can define broadcast routes in

routes/channels.php

you can then define events that by default include broadcast method in scaffolding.

public function broadcastOn()
{
    return new PrivateChannel('channel-name');
}

Bind listeners to event and implement "shouldque" interface to that these run as async jobs. You can also use laravel notifications to provide live notification. laravel broadcasting

If you still want to grab event in controller you can specify in EventServiceProvider as

class EventServiceProvider extends ServiceProvider
{

    protected $listen = [
        DownloadFile::class => [
          CompanyDashboardController::class,
        ],
    ];

    public function boot()
    {
        parent::boot();
    }
}

your event will look like

class DownloadFile
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $process_id;

    public function __construct($process_id)
    {
        $this->process_id = $file;
    }
}

Finally you can grab this event in you controller as like:

class CompanyDashboardController extends Controller
{

    public function __construct()
    {
      //constructor
    }
    public function handle(DownloadFile $event)
    {
        if($event->process_id == 1)
        {
            return "something";
        }

    }
}

If you meant to some Javascript event instead of Laravel read Use Server-Sent Events to push messages to browser

Arsii Rasheed
  • 324
  • 1
  • 5
  • 18
  • 1
    the channel way is via websocket, but for the SSE, I'm talking about the Laravel SSE(server sent event) solution, how can the server side event listener trigger the SSE and send to frontend? – eversun Oct 28 '19 at 19:32
  • you can specify controller instead of listener in EventServiceProvider.php, however for this you will need handle() function in your controller. will it work for you ? – Arsii Rasheed Oct 29 '19 at 16:49