dragon4808 2019-04-18 15:05
浏览 65
已采纳

laravel和反应按钮设置

I am working on a web project.

I have laravel as my back-end reactjs as my front-end

I have users, posts comments in my DB.

I wanted to implement pusher so i can have this real time posts showing up once any user posted a new post.

I am very close to achieve this behavior.

on laravel

i have this event

class NewPostsCast implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $post;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($post)
    {
        $this->post = $post;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('posts');
    }

} 

and once the post stored to my database I trigger this event

event(new NewPostsCast($output)); 

on react

let channel = pusher.subscribe("posts");
// i guess error is here because this function never called
// it's only called at startup
channel.bind("NewPostsCast", post => {
    console.log("pusher::: ", post);
});

might be useful

from pusher logs

Pusher : No callbacks on posts for App\Events\NewPostsCast

however pusher returns this log also

Pusher : Event recd : {"event":"App\\Events\\NewPostsCast","channel":"posts","data":{"post":{"id":19,"title":"sd","body":"eksdl","tags":"[\"ds\",\"dsf\"]","user_id":1,"created_at":"2019-04-18 14:22:00","updated_at":"2019-04-18 14:22:00","votes":0,"user":{"id":1,"name":"user1","username":"user1","email":"user1@health.io","address":null,"state":null,"country":null,"gender":"female","phone":null,"avatar":"http://healthqo.api/public/profile_pics/default/female.png","created_at":"2019-04-09 08:30:12","updated_at":"2019-04-09 08:30:12"}}}}
  • 写回答

1条回答 默认 最新

  • duan19911992 2019-04-19 11:17
    关注

    in case anyone is still struggling with the same problem

    from pusher logs it says (No Callbacks on posts for App\Events\NewPostsCast) that quite solves the problem

    since react and laravel are separated in my project (NewPostsCast) is not defined so i had to change first parameter of the bind function

    like so:
    FROM

    let channel = pusher.subscribe("posts");
    channel.bind("NewPostsCast", post => {
        console.log("pusher::: ", post);
    });
    

    TO

    let channel = pusher.subscribe("posts");
    channel.bind("App\\Events\\NewPostsCast", post => {
        console.log("pusher::: ", post);
    });
    

    not sure why double double back slashes but it gives me a syntax error with single back slash

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题