使用Rxjs或其他方法监听Angular中的数据库数据更改 [英] Listening for DB data changes in Angular using Rxjs or anything

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

问题描述

我在后端(node.js)中有一个服务,我正在通过角订阅 http客户端服务,它工作正常.对于我的应用程序,有多个用户. ,以便其他一些用户可以更改数据库数据.因此,无论何时发生DB数据更改,我都希望立即订阅它们.,以便所有用户都可以查看一致的数据,即与DB数据同步的数据. 我在角度服务中使用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请求,即,服务器从不发送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(...)
)

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

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天全站免登陆