noisim/ss-event

This library provides Server Sent Event for Laravel.

dev-master 2017-10-20 10:59 UTC

This package is auto-updated.

Last update: 2021-11-27 17:29:31 UTC


README

This library provides Server Sent Event for Laravel. Tested on Laravel 5.5.

How to use

This package is installed via Composer. To install, simply add it to your composer.json file:

{
    "require": {
        "noisim/ss-event": "dev-master"
    }
}

and run composer to update the dependencies composer update.

Then open your Laravel config file config/app.php and in the $providers array add the service provider for this package.

\Noisim\SSEvent\SSEventServiceProvider::class

Finally generate the configuration file running in the console:

php artisan vendor:publish --tag=config

Laravel Controller:

<?php
namespace App\Http\Controllers;

use Noisim\SSEvent\SSEvent;

class TestController extends Controller
{
    public function test()
    {
        $sse = new SSEvent();
        return $sse->sleepTime(10)->addEvent("event_name", function () {
            return json_encode(["hello" => "world"]);
        })->start();
    }
}

Laravel Route:

<?php

Route::get('test', 'TestController@test')->name("test");

Client Javascript:

var source = new EventSource('/test');
source.addEventListener('event_name', function(event) {
     console.log(event);
}, false);