【问题标题】:Real-Time notification using laravel as back-end API and AngularJS as front-end使用 laravel 作为后端 API 和 AngularJS 作为前端的实时通知
【发布时间】:2018-10-06 16:55:12
【问题描述】:

后端 api 是 laravel 时,是否可以在 AngularJS 中使用实时通知?我应该在 AngularJS 中使用 laravel echo 还是有其他解决方案?

正如我所搜索的,我应该使用 pusher 作为 websocket 服务器,并且应该使用 laravel echo 这是一个 JS 库。

问题是我应该在 AngularJS 中使用 laravel-echo 库还是有其他解决方案?

仅供参考:laravel(后端)api 与 AngularJS(前端)分离,它们通过使用 O-Auth 进行身份验证的 CRUD 请求进行通信。

【问题讨论】:

  • 你可以使用socket.IO,可以与laravel集成,查看这篇文章,medium.com/@adnanxteam/…
  • @Luillyfe 好点,我会检查一下。

标签: php angularjs laravel laravel-5 notifications


【解决方案1】:

我已经设法使用 laravel + redis + socket.io + socket 客户端解决了这个问题,我把我的解决方案放在这里希望不要再浪费其他人的时间来解决这个问题。

cd your-project-name

安装包

npm install express ioredis socket.io --save

你的 package.json 文件看起来像

{
  "private": true,
  "devDependencies": {
    "gulp": "^3.8.8",
    "laravel-elixir": "*"
  },
  "dependencies": {
    "express": "^4.12.4",
    "ioredis": "^1.4.0",
    "redis": "^0.12.1",
    "socket.io": "^1.3.5"
  }
}

composer require predis/predis

创建事件

php artisan make:event EventName

确保它implements ShouldBroadcast

整个 EventName.php 类应该如下所示:

<?php namespace App\Events;

use App\Events\Event;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;

class EventName extends Event implements ShouldBroadcast
{
    use SerializesModels;

    public $data;

    public function __construct()
    {
        $this->data = array(
            'power'=> '10'
        );
    }

    public function broadcastOn()
    {
        return ['test-channel'];
    }
}

在 Angular js 中使用此代码:

<script src="js/socket.io.js'"></script>
    <script>
        //var socket = io('http://localhost:3000');
        var socket = io('http://192.168.10.10:3000');
        socket.on("test-channel:App\\Events\\EventName", function(message){
            // increase the power everytime we load test route
            $('#power').text(parseInt($('#power').text()) + parseInt(message.data.power));
        });
    </script>

路由 像这样设置三个路线。将它们添加到您的 app/Http/routes.php 文件中。

Route::get('/', function() {
    // this doesn't do anything other than to
    // tell you to go to /fire
    return "go to /fire";
});

Route::get('fire', function () {
    // this fires the event
    event(new App\Events\EventName());
    return "event fired";
});

Route::get('test', function () {
    // this checks for the event
    return view('test');
});

在您的项目根目录中创建socket.js 文件

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel', function(err, count) {
});
redis.on('message', function(channel, message) {
    console.log('Message Recieved: ' + message);
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});
http.listen(3000, function(){
    console.log('Listening on Port 3000');
});

仅供参考:确保您已安装 redis 服务器 + 使用 .env 或数据库文件将其连接到 Laravel

【讨论】:

  • 我应该在哪里包含socket.js
  • 在你的项目根目录中。
  • 我收到错误Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.10.10:3000/socket.io/?EIO=3&amp;transport=polling&amp;t=MFjDQl0. (Reason: CORS request did not succeed).
  • 现在工作正常.. 非常感谢.. @Mohammad_Hosseini 为我节省了很多时间.. 谢谢
  • @shihabudheen 如果有帮助,请随时投票。
【解决方案2】:

我之前使用过@Mohammad_Hosseini 的解决方案,但是在将 Laravel 5.4 升级到 5.8 之后,它就不再工作了。 最后但并非最不重要的; @Mohammad_Hosseini 的解决方案不包括授权和私人频道。

所以,我强烈建议使用 laravel-echo-server 来轻松升级。此外,它是由伟大的社区开源和维护的。

【讨论】:

  • 这不是答案。请删除或改成一个:stackoverflow.com/help/how-to-answer
  • 感谢您提到它在 laravel 5.8 中不起作用,我会在找到空闲时间后立即研究。但这不是答案,应该写成评论。
  • 请注意,这是一个运行您自己项目的套接字服务器的解决方案,不使用任何第三方。
猜你喜欢
  • 2020-10-03
  • 2014-12-26
  • 2016-11-15
  • 2018-08-30
  • 2022-06-19
  • 2014-09-24
  • 1970-01-01
  • 2014-07-27
  • 2013-07-10
相关资源
最近更新 更多