想在 Laravel 中实现 web sockets [英] Want to implement web sockets in Laravel

查看:39
本文介绍了想在 Laravel 中实现 web sockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用网络套接字在 Laravel 中实现网络通知.我有试过推.这很容易,但它是一个付费套餐.我也尝试过 redis 和 socket.io,我必须为其安装地平线.我正在运行 Windows,但根据我对地平线的了解,我无法在 Windows 上安装它.

I want to implement web notifications in Laravel using web sockets. I have tried pusher. It is very easy but it is a paid package. I have also tried redis and socket.io for which I have to install horizon. I am running Windows and I cannot install it on Windows according to what I have read about horizon.

所以我对推送通知很困惑.我尝试了至少一周,但还没有找到任何解决方案.我的前端是angluar 5/android,后端是Laravel 5.6.

So I am very confused about push notification. I am trying for at least one week but haven't found any solution yet. My front end is in angluar 5/android and backend is in Laravel 5.6.

你能给我推荐一些实现推送通知的好主意吗?

Can you please suggest me any good ideas to implement push notification?

推荐答案

你一直在使用 pusher,所以我假设你知道如何使用事件.

You have been use pusher,so I assume you know how to use event.

我推荐你使用 laravel-echo-server.它非常易于使用,带有内置的 api.

I recommend you laravel-echo-server.It's very easy to use,with a built-in api.

这是一个全新的 laravel-echo 项目示例.设置项目后.

Here is a fresh example of a laravel-echo project from scratch. After you set up the project.

如果你还没有安装 predis,你将需要它

you will need predis if you haven't install it yet

composer require predis/predis
redis-server /usr/local/etc/redis.conf

  1. 运行 npm install -g laravel-echo-server
  2. 运行 laravel-echo-server init
  3. config/app.php
  4. 中取消注释AppProvidersBroadcastServiceProvider::class
  5. 添加<代码><script src="https://cdn.jsdelivr.net/npm/socket.io-client@2.1.1/dist/socket.io.js"></script> 在你的 app.blade.php 中
  6. 你需要一个 socketio-client ,你可以运行 npm install --save laravel-echo .如果你不熟悉 npm 或 vue,你可以简单地从我的 github 中包含这个编译文件项目.已编译的js
  7. 将此添加到 app.blade.php

  1. run npm install -g laravel-echo-server
  2. run laravel-echo-server init
  3. uncomment AppProvidersBroadcastServiceProvider::class in config/app.php
  4. add <script src="https://cdn.jsdelivr.net/npm/socket.io-client@2.1.1/dist/socket.io.js"></script> in your app.blade.php
  5. you need a socketio-client ,you can run npm install --save laravel-echo .In case you are not familiar with npm or vue, you can simply include this compiled file from my github project.compiledjs
  6. add this to app.blade.php

<script>
window.Echo = new Echo({
broadcaster: 'socket.io',

host: '{{url('/').':6001'}}',
});
Echo.private(`App.User.{{Auth::id()}}`) // private channel
.listen('NewMessage', (e) => {
    console.log(e)

});


</script>

  • 最后运行 laravel-echo-server start 并打开你的项目,你会看到

  • finally run laravel-echo-server start and open you project ,you will see

    [20:53:21] - Lv5OKDAcuSLsK1nBAAAE authenticated for: private- 
       App.User.1
    [20:53:21] - Lv5OKDAcuSLsK1nBAAAE joined channel: private-App.User.1
    

  • 您可以收听您想要的任何事件或其他通知

  • you can listen any event you want or other notifications

    这是我的 github 项目 echo-example

    here is my github project echo-example

    添加一些代码,以便您可以使用邮递员向特定用户发送消息.您可以从chrome控制台看到消息.您可以从github项目截图中获得更多详细信息

    add a little code so that you can use postman send message to specfic user.You can see the message from chrome console.You can get more details from github project screenshots

    这篇关于想在 Laravel 中实现 web sockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆