【问题标题】:Pusher: Difference between channel binding and Laravel echoPusher:通道绑定和 Laravel 回显的区别
【发布时间】:2019-10-02 13:14:40
【问题描述】:

我最近使用 Pusher 和 Laravel 开发了一个通知系统。不幸的是不能让它这样工作:

 import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

Pusher.logToConsole = true;

 window.Echo = new Echo({
     broadcaster: 'pusher',
    key: 'WORKING KEY ..',
     cluster : "mt1",
     encrypted: true

 });

window.Echo.channel('post')
    .listen('ArticleEvent', function (e) {

    console.log(e);

});

虽然消息已发送到客户端控制台,但 Listen 根本没有工作......并且没有任何记录。

反正我是这样用的,效果很好:

window.Pusher = require('pusher-js');

var pusher = new Pusher('WORKING KEY ..', {
    encrypted: true,
    cluster: 'mt1',
});

var channel = pusher.subscribe('post');

channel.bind('ArticleEvent', function(e) {
    alert(JSON.stringify(e['message']));
});

两种方式之间的区别是什么?必须优先选择哪种方式?

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow,请定义无效
  • @CaddyDZ Tnx,我不是新来这里的,我已经失去了我的旧帐户......正如我所提到的,Listen 根本不起作用。 window.Echo.channel('post') 工作正常,但在 Listen 部分,代码似乎停止了,没有任何东西传递给 function(e)

标签: laravel pusher


【解决方案1】:

您需要在 Listen 方法中包含命名空间信息。

请尝试使用(注意. 字符):

window.Echo.channel('post')
    .listen('.ArticleEvent', function (e) {

    console.log(e);

});

【讨论】:

  • 谢谢!它起作用了..我是否需要将通知存储在数据库中以供离线用户使用? (所以当他们重新上线时可以看到通知)。
  • 正确,Channels 不存储任何消息,仅将它们发布给订阅的客户端。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 2016-03-18
  • 2017-07-06
  • 1970-01-01
相关资源
最近更新 更多