I'm just starting to familiarize with AngularJS, but I would like to build a web app that has a view that gets auto-upated in real-time (no refresh) for the user when something changes in the server-side database.

我刚刚开始熟悉AngularJS,但是我想构建一个web应用程序,当服务器端数据库中发生更改时,该应用程序的视图会自动更新(不刷新)。

Can AngularJS handle this (mostly) automatically for me? And if so, what is the basic mechanism at work?

AngularJS能帮我自动处理这个(大部分)吗?如果是的话,那么基本的机制是什么呢?

For example, do you somehow setup AngularJS to poll the DB regularly for "model" changes? Or use some sort of Comet-like mechanism to notify AngularJS client-side code that the model has changed?

例如,您是否设置了AngularJS定期轮询DB以获取“模型”更改?或者使用某种类似cometlike的机制来通知AngularJS客户端代码模型已经更改?

In my application, the challenge is that other (non-web) server-side software will be updating the database at times. But this question applies equally to pure web-apps where you might have multiple clients changing the database through AngularJS web clients, and they each need to be updated when one of them makes a change to the DB (model).

在我的应用程序中,挑战在于其他(非web)服务器端软件有时会更新数据库。但是这个问题同样适用于纯web应用程序,在这些应用程序中,您可能会有多个客户机通过AngularJS web客户端更改数据库,并且当其中一个客户机对DB(模型)进行更改时,它们都需要进行更新。

5 个解决方案

#1


96

You have a few choices...

你有一些选择……

  1. You could do polling every X milliseconds using $timeout and $http, or if the data you're using is hooked up to a REST service, you could use $resource instead of $http.

    您可以使用$timeout和$http对每一个X毫秒进行轮询,或者如果您使用的数据连接到REST服务,您可以使用$resource而不是$http。

  2. You could create a service that uses some Websocket implementation and uses scope.$apply to handle changes that are pushed by the socket. Here's an example using socket.io, a node.js websocket library:

    您可以创建一个使用某些Websocket实现并使用范围的服务。$apply用于处理套接字推动的更改。这里有一个使用套接字的示例。io,一个节点。js websocket库:

    myApp.factory('Socket', function($rootScope) {
        var socket = io.connect('http://localhost:3000');
    
        //Override socket.on to $apply the changes to angular
        return {
            on: function(eventName, fn) {
                socket.on(eventName, function(data) {
                    $rootScope.$apply(function() {
                        fn(data);
                    });
                });
            },
            emit: socket.emit
        };
    })
    
    function MyCtrl($scope, Socket) {
        Socket.on('content:changed', function(data) {
            $scope.data = data;
        });
        $scope.submitContent = function() {
            socket.emit('content:changed', $scope.data);
        };
    }
    
  3. You could get really high tech and create a websocket implementation which syncs an Angular model with the server. When the client changes something, that change gets automatically sent to the server. Or if the server changes, it gets sent to the client.
    Here's an example of that in an old version of Angular, again using socket.io: https://github.com/mhevery/angular-node-socketio

    您可以获得非常高的技术,并创建一个websocket实现,它与服务器同步一个角度模型。当客户端更改某些内容时,该更改将自动发送到服务器。或者,如果服务器发生变化,它将被发送到客户端。这是一个旧版本的角的例子,同样使用套接字。io:https://github.com/mhevery/angular-node-socketio

EDIT: For #3, I've been using Firebase to do this.

编辑:对于#3,我一直在使用Firebase做这个。

更多相关文章

  1. 在两台服务器上有效地在两个Django应用程序之间进行通信(多租户)
  2. 如何停止Py_Initialise应用程序的崩溃?
  3. 轮询Web服务的最佳方式(例如,对于Twitter应用程序)
  4. 乘客的Django应用程序显示空主页
  5. 用于在Google App Engine上构建应用程序的最佳开源IDE?
  6. 应用Python开发WebService服务端及客户端
  7. python模拟mysql多客户端并发操作
  8. linux socket网络编程:fcntl select(多个客户端连接服务器端情形)
  9. 获取Android应用程序的Linux UID

随机推荐

  1. Linux服务器查看 PHP 是否支持mail()函数
  2. PHP中如何通过getopt解析GNU C风格命令行
  3. PHP-Ajax实现异步上传图片到新浪图床
  4. 10 个 PHP 常见安全问题(实例讲解)
  5. nginx禁止指定目录运行php
  6. PHP-xml & jsonp转数组的方法
  7. 如何开启mysql和php慢日志
  8. php读取大文件的行数的方法
  9. PHP获取字符串中字符、字符串第n次出现的
  10. 用php输出一个数组中的偶数或奇数的方法