MAnfal / sse

SSE is a Laravel wrapper for PHP Library LibSSE
MIT License
5 stars 3 forks source link

How to use with laravel Events ? #2

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 extends SSEEvent
{
    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 extends SSEEvent
{
    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

jrganfal commented 6 years ago

hi @sarfraznawaz2005 its been a long time since I worked on this repo, if you are still interested in the solution lemme know.

sarfraznawaz2005 commented 6 years ago

@jrganfal I have stopped using it because of no solution and switched to websockets instead. However you can put up a solution/readme of how to do that in laravel for someone else who can use it. I tried but could not get it to work with laravel events, iam sure i must have missed something when i looked into it.

Thanks

atefBB commented 5 years ago

@jrganfal I work with it, and I wonder what is the solution?! Thx.

MAnfal commented 5 years ago

Hi @atefBB thanks for your comment, I'll take a look if it needs an update in the logic of the package and update you here. I haven't maintained this package for a long time but I think I should.

atefBB commented 5 years ago

@MAnfal This is a very useful package! Thank you Bro.

جزاك الله خيرا.