tonyhhyip / laravel-sse

libSSE-php for Laravel framework
https://gitlab.com/tonyhhyip/laravel-sse
14 stars 4 forks source link

How to use with laravel Events ? #3

Open sarfraznawaz2005 opened 7 years ago

sarfraznawaz2005 commented 7 years ago

Hi, thanks for the libaray !

I am to send messages and everything works but I want to ask how to send SSE event when some laravel events happens such as when a user logs in or when a new record of certain model is created.

I tried below code to send SSE event when a user logged in but it doesn't work:

class MyEvent implements Event
{
    public function check()
    {
        // send login notification
        Event::listen('auth.login', function(){
           return true;
        });

        // default
        return false;
    }

    public function update()
    {
        return 'A new user just logged in.';
    }
}

If I return true directly from check method, I can see the A new user just logged in coming in browser, however I want to be able to do so only when a user logs in on the auth.login event of laravel.

I also tried for my custom model events, they also don't work:

class MyEvent implements Event
{
    public function check()
    {
        // send message notification
        Message::created(function(){
           return true;
        });

        // default
        return false;
    }

    public function update()
    {
        return 'You have received a new message';
    }
}

Question:

How to make this library work with Laravel Events or how to handle laravel events logic in the check method ?

Thanks

hxshandle commented 7 years ago

hi @sarfraznawaz2005 I met the same problem do you have the solution about it? -Handle

sarfraznawaz2005 commented 7 years ago

@hxshandle nope no solutions yet so i started using websockets http://codeinphp.github.io/post/sockets-with-php-and-node/