Laravel 事件未广播

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41218171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:57:24  来源:igfitidea点击:

Laravel event not being Broadcast

laraveleventsvue.jslaravel-echo

提问by AshMenhennett

I am working on a chat/message feature for an app. When a User sends a message, I want a specific Event to be triggered and to be broadcasted. I am then wanting Laravel Echo to listen on the event's broadcast channel to an event via Laravel Echo, using the pusherdriver.

我正在为应用程序开发聊天/消息功能。当用户发送消息时,我希望触发和广播特定事件。然后我希望 Laravel Echo 使用pusher驱动程序通过 Laravel Echo 在事件的广播频道上收听事件。

I am wanting the Laravel Echo implementation to be in a Vue component and to retrieve the data that was broadcasted via the event. Currently, I can't get an event to broadcast to either a private channel or just a channel.

我希望 Laravel Echo 实现位于 Vue 组件中并检索通过事件广播的数据。目前,我无法将事件广播到私人频道或仅频道。

Here is what I have done:

这是我所做的:

1) Added the config to .env.

1) 将配置添加到.env.

2) Added config to bootstrap.js:

2)将配置添加到bootstrap.js

window.Echo = new Echo({
  broadcaster: 'pusher',
  key: 'mykey',
  encypted: false
});

3) Added auth check in App\Providers\BroadcastServiceProvider(just returning true if User is signed in for now):

3) 添加了身份验证签入App\Providers\BroadcastServiceProvider(如果用户现在已登录,则仅返回 true):

Broadcast::channel('chat', function ($user) {
  // verify that user is recipient of message
  return \Auth::check();
});

4) Added App\Providers\BroadcastServiceProvider::class,to App/config/app.php.

4) 添加App\Providers\BroadcastServiceProvider::class,App/config/app.php.

5) Created the Event App\Events\UserSentMessage, which broadcasts on a PrivateChannelnamed chat. I have also tried to broadcast to a Channel. I checked and the event is being fired from a call to the event()helper in a controller, it just seems to not be broadcast. I have also tried using the broadcastmethod in the controller mentioned.

5) 创建了 Event App\Events\UserSentMessage,它在一个PrivateChannel命名的chat. 我也试过广播到Channel. 我检查了一下,该事件是通过调用event()控制器中的助手来触发的,它似乎没有被广播。我也尝试使用broadcast提到的控制器中的方法。

6) Added the following within the mountedVue life cycle hook in a valid Vue component (component is rendering data etc as expected, just not working with Laravel Echo atm):

6)mounted在有效的 Vue 组件中的Vue 生命周期钩子中添加了以下内容(组件按预期呈现数据等,只是不适用于 Laravel Echo atm):

    Echo.private('chat').listen('UserSentMessage', (data) => {
                console.log(data);
            }, (e) => {
                console.log(e);
            });

Have also tried:

也试过:

     Echo.channel('chat').listen('UserSentMessage', (data) => {
                console.log(data);
            }, (e) => {
                console.log(e);
            });

The Vue component is successfully subscribing to the channel specified. The pusher debug console recognized that it is being subscribed to, but the Event, when triggered never gets broadcasted.

Vue 组件成功订阅了指定的频道。推送器调试控制台识别出它正在被订阅,但事件在触发时永远不会被广播。

Seems to be that there is a problem with broadcasting the event. I have also tried using the broadcast()helper, rather than the event()helper.

好像是广播事件有问题。我也尝试过使用broadcast()助手,而不是event()助手。

Also, when subscribing to the event with Laravel Echo via Echo.private(). It seems to prefix the channel name with private, so I have also tried broadcasting (in the event) to a channel named private-chat.

此外,当通过 Laravel Echo 订阅事件时Echo.private()。它似乎在频道名称前加上private,所以我也尝试广播(在事件中)到一个名为 的频道private-chat

回答by num8er

I can only recommend You to check Your .envfile
and make sure BROADCAST_DRIVERis not log(try pusher)
and also don't forget to keep running queue listener:

我只能建议您检查您的.env文件
并确保BROADCAST_DRIVER不是log(尝试pusher
并且不要忘记继续运行队列侦听器:

php artisan queue:listen

回答by materliu

the event will not be broadcasted immediately, it will be send to the queue, only when you listen or work on the queue, the event will be consumed. If you want execute jobs immediately, set QUEUE_DRIVER=sync . see: laravel queue

该事件不会立即广播,它会发送到队列中,只有当您侦听或在队列上工作时,才会消耗该事件。如果您想立即执行作业,请设置 QUEUE_DRIVER=sync 。参见:laravel 队列

回答by sba

@materliu: that was a good hint. In my case i did not create a queue with the working directory set to the project i am working on. Using circus. The event was triggered in code but no job listener was available. As soon as i run "php artisan queue:listen" suddenly all previous broadcasts have been processed.

@materliu:这是一个很好的提示。在我的情况下,我没有创建一个工作目录设置为我正在处理的项目的队列。使用马戏团。该事件是在代码中触发的,但没有可用的作业侦听器。当我运行“php artisan queue:listen”时,所有以前的广播突然都被处理了。

Usually its difficult to figure out which component of a complex system does not work.

通常很难弄清楚复杂系统的哪个组件不起作用。

回答by omarjebari

I have upvoted Num8er's reply but i also found some other issues so maybe they can help you too.

我已经对 Num8er 的回复投了赞成票,但我也发现了其他一些问题,所以也许他们也可以帮助你。

1) Remove all 'private-' from the beginning of your channel names. These get handled behind the scenes by the various libraries (eg socket.io/laravel-echo). If you look at any debug output you will see 'private-' prepended to the front of the channel names but you DO NOT need to add these.

1) 删除频道名称开头的所有“private-”。这些由各种库(例如 socket.io/laravel-echo)在幕后处理。如果您查看任何调试输出,您将看到“private-”位于通道名称的前面,但您不需要添加这些。

2) If you're using redis then make sure the REDIS_PREFIX is set to an empty string in your .env file. By default it's set to something like 'laravel_database' and this messes up the channel names.

2) 如果您使用的是 redis,请确保在您的 .env 文件中将 REDIS_PREFIX 设置为空字符串。默认情况下,它设置为“laravel_database”之类的内容,这会弄乱频道名称。

REDIS_PREFIX=

3) Make sure you've got your laravel-echo server running (and redis/pusher). Also you need to make sure you have the queue worker running:

3)确保你的laravel-echo服务器正在运行(和redis/pusher)。您还需要确保队列工作人员正在运行:

php artisan queue:work

4) It's worth enabling debugging output on your Echo Server as this will tell you useful info about traffic (eg channel names) so modify the laravel-echo-server.json file (in your project root dir) so that devMode is set to true:

4) 值得在 Echo Server 上启用调试输出,因为这会告诉您有关流量的有用信息(例如通道名称),因此请修改 laravel-echo-server.json 文件(在您的项目根目录中),以便将 devMode 设置为 true :

"devMode": true,