Laravel:使用 Angular 制作实时应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24835603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:49:57  来源:igfitidea点击:

Laravel: Making a Real Time Application using Angular

angularjslaravelfirebasepusherpubnub

提问by LoveAndHappiness

I am starting to work with angular and am fascinated by the bi-directional data-binding capabilities and by its $http method, which lets me save changes in to my mysql database, without refreshing the page.

我开始使用 angular 并且对双向数据绑定功能及其 $http 方法着迷,它让我可以将更改保存到我的 mysql 数据库中,而无需刷新页面。

Another thing I am currently fascinated by is the real time capability across multiple clients using firebase. Here all clients are updated in REAL TIME, when the database receives any changes. I'd probably like to use firebase, but I would have to drop Laravel and MySql as a persistence layer entirely, which I would like to keep for the moment, since my application is already working in Laravel, just not in real time.

我目前着迷的另一件事是使用 firebase 的跨多个客户端的实时功能。在这里,当数据库收到任何更改时,所有客户端都会实时更新。我可能想使用 firebase,但我必须完全放弃 Laravel 和 MySql 作为持久层,我想暂时保留它,因为我的应用程序已经在 Laravel 中运行,只是不是实时的。

How would I go about having a Real Time application, which updates every client, without refreshing the view, in Laravel using MySQL and Angular?

我将如何使用 MySQL 和 Angular 在 Laravel 中拥有一个实时应用程序,该应用程序更新每个客户端,而无需刷新视图?

If I am not mistaken, Pusher and PubNub, are providing this necessary open connection with the server using websockets, so when the server has something to share, angular will now and render it.

如果我没记错的话,Pusher 和 PubNub 正在使用 websockets 提供与服务器的这种必要的开放连接,所以当服务器有东西要共享时,angular 现在会渲染它。

Since I would like to use Laravel and MySQL as a persistence layer, I am not sure, what the best way would be. I am not even sure, if I understood everything correctly, which I wrote above, since I am new to angular and real-time applications.

由于我想使用 Laravel 和 MySQL 作为持久层,我不确定最好的方法是什么。我什至不确定,我是否正确理解了上面写的所有内容,因为我是角度和实时应用程序的新手。

What would be the next necessary steps, to get some Real-Time capability into a PHP/MySQL application?

为了在 PHP/MySQL 应用程序中获得一些实时功能,接下来的必要步骤是什么?

采纳答案by Claudivan

The solution for your problem is:

您的问题的解决方案是:

1o - open websocket connection with the websocket-server and subscribe a channel, after this send the data to your serve using ajax tutorial angular pusher

1o - 打开与 websocket-server 的 websocket 连接并订阅一个频道,然后使用 ajax教程 angular pusher将数据发送到您的服务器

2o - In server side, you get the data, saves to your database and send a 'PUBLISH' to the respective channel into websocket server lib useful for this

2o - 在服务器端,您获取数据,保存到您的数据库并将“发布”发送到相应的通道到 websocket 服务器 库中,这对此很有用

3o - Through the subscribe gets the data in real time

3o - 通过订阅实时获取数据

Pusher.subscribe('channel', 'event', function (item) {
    // code
});

回答by schrink

I had a similar problem recently and I finally ended up using Redis publish/subscribe Redis. You can store data in the channel and then subscribe to any changes. When something changes you can send it to Pusher which will send it then to the clients.

我最近遇到了类似的问题,最后我最终使用了 Redis 发布/订阅Redis。您可以将数据存储在频道中,然后订阅任何更改。当某些事情发生变化时,您可以将其发送给 Pusher,然后将其发送给客户端。

I also recommend considering Node.js and Socket.io since you can achieve very good performance without third party service, and even if you don't have experience with node you can find very good examples on Socket.IOhow to write an application.

我还建议考虑 Node.js 和 Socket.io,因为您无需第三方服务即可获得非常好的性能,即使您没有使用 node 的经验,您也可以在Socket.IO上找到非常好的示例,如何编写应用程序。

For Redis there is a good library for PHP called Predis and there is Redis Node client as well, so you can mix it all together.

对于 Redis,有一个很好的 PHP 库,称为 Predis,还有 Redis Node 客户端,因此您可以将它们混合在一起。