使用 Rxjs 或任何东西侦听 Angular 中的数据库数据更改 [英] Listening for DB data changes in Angular using Rxjs or anything

查看:17
本文介绍了使用 Rxjs 或任何东西侦听 Angular 中的数据库数据更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在后端(node.js)中有一个服务,我正在通过 angular 订阅它http 客户端服务,它工作正常.对于我的申请,有不止一个用户.所以其他一些用户可以更改数据库数据.所以每当数据库数据发生变化时,我都会立即订阅它们. 这样所有用户都可以查看一致的数据,即与数据库数据同步的数据.我在角度服务中使用 RXJS Observables.

i have a Service in backend (node.js) i am subscribing it through the angular http client service and it is working fine. for my application more than one uers are there. so some other user can change the DB data . so whenever the changes happened to DB data i would like subscribe them immediately. so all users can view the consistent data i.e., data synchronized with DB data. i am using RXJS Observables in angular services.

推荐答案

在回答之前,请注意满足您的要求说起来容易做起来难.为现有基础架构实现这一点需要大量工作,如果您绝对需要这样做,最好在服务器端使用现有框架.

Before going into the answer, be aware that fulfilling your requirement is easier said than done. Implementing this for an existing infrastructure will be a lot of work and if you absolutely need to do this, it's probably better you use existing frameworks on your server-side.

HTTP 不是执行此任务的正确协议类型.它基于请求/响应,并希望客户端知道何时它想要请求信息.

HTTP is not the right kind of protocol for this task. It is request/response based and expects the client to know when it would like to request the information.

如果一定要使用HTTP,我只能想到两种方法.

If you must use HTTP I can only think of two ways.

选项 1: 创建一个永远不会关闭的 HTTP 请求,即在 express 中,服务器永远不会发送 res.end() 或类似的东西.这种方法也需要一个自定义的 HTTP 客户端,因为默认的 Angular HTTP 客户端希望请求在向订阅者发送任何通知之前结束.但是,如果服务器没有发送数据,浏览器可能会终止请求,因此您必须自己实现保持活动状态.

Option 1: Create a single HTTP request that is never closed, i.e. in express, the server never sends res.end() or something like that. This approach requires a custom HTTP Client as well, as the default Angular HTTP Client expects the request to end before sending any notification to subscribers. However, browsers may kill off a request if no data is sent from the server, so you must implement a keep-alive yourself.

选项 2: 使用轮询" - 这意味着您不断询问您的服务器数据是否已更改.这可以在 RxJS 中实现:

Option 2: Use "polling" - which means that you constantly ask your server if data has changed. This can be implemented in RxJS with:

interval(1000).pipe
  switchMap(() => this.http.get(...)
)

这带来了一个巨大的缺点,即您的服务器(以及您的数据库)必须处理大量请求并且您的网络流量增加(尽管只发送小包).但是,这对您来说可能是最容易实现的,因为几乎不需要任何更改来支持这一点.请注意,如果您同时拥有过多用户,您的服务器和数据库会在某个时候出现故障.

This comes with the huge drawback that your server (and also your DB) has to handle a lot of requests and your network traffic increases (though only small packages are sent). However, this will probably be the easiest to implement for you as almost nothing has to be changed to support this. Be aware that your server and database will go down at some point if you have too many users at the same time.

浏览器只支持这么多协议,因此只有有限数量的解决方案可以满足您的需求.一种是WebSocket.它基本上是浏览器上的 TCP 协议,但允许长期连接,服务器可以选择推送数据到客户端.RxJS 中甚至有一个类,称为 WebSocketSubject.

The browser supports only so many protocols, so there are only a limited number of solutions for your requirement. One is WebSocket. It is basically the TCP protocol over the browser, but allows for long-living connections with the option for the server to push data to the client. There is even a class in RxJS for it, called WebSocketSubject.

现在,这种方法的伸缩性比任何 HTTP 方法都要好,但可能需要您重建后端的大部分内容.如果您想更深入地了解这一点,您可以从这里开始.

Now, this approach scales better than any HTTP approach, but probably requires you to rebuild huge parts of your backend. If you want to go deeper into this, you can start here.

到目前为止,我们只讨论了网络通信,当然您的数据库还必须支持可观察的 API(轮询方法除外).您需要查看您的特定数据库系统的文档以了解它是否存在.

We've only talked about network communication so far, but of course your database also has to support an observable API (except for the polling approach). You need to look into the documentation of your specific database system to find out if it does.

这篇关于使用 Rxjs 或任何东西侦听 Angular 中的数据库数据更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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