想要在Laravel中实现Web套接字 [英] Want to implement web sockets in Laravel
问题描述
我想使用Web套接字在Laravel中实现Web通知.我有 尝试过的推杆.这很容易,但是它是一个付费的程序包.我还尝试了redis和socket.io,必须为其安装Horizon.我正在运行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
- 运行
npm install -g laravel-echo-server
- 运行
laravel-echo-server init
- 取消注释
config/app.php
中的App\Providers\BroadcastServiceProvider::class
- 在您的app.blade.php中添加
<script src="https://cdn.jsdelivr.net/npm/socket.io-client@2.1.1/dist/socket.io.js"></script>
- 您需要一个socketio-client,可以运行
npm install --save laravel-echo
.如果您不熟悉npm或vue,则可以从我的github项目中简单地包含此编译文件. .com/evolot/echoexample/blob/master/public/js/echocompiled.js"rel =" nofollow noreferrer> compiledjs -
将此添加到app.blade.php
- run
npm install -g laravel-echo-server
- run
laravel-echo-server init
- uncomment
App\Providers\BroadcastServiceProvider::class
inconfig/app.php
- add
<script src="https://cdn.jsdelivr.net/npm/socket.io-client@2.1.1/dist/socket.io.js"></script>
in your app.blade.php - 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 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套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!