Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

angular - 如何将消息从服??务器发送到Web应用程序(角度)(How to send messages from server to web application (angular))

Usually, An application communicates with the server by sending HTTP requests.

(通常,应用程序通过发送HTTP请求与服务器进行通信。)

It sends some data over a HTTP request and receives a response in turn.

(它通过HTTP请求发送一些数据,然后依次接收响应。)

But when building my application, I ran into a situation where the backend needs to send some data/message to the web application when a particular event occurs.

(但是在构建我的应用程序时,我遇到一种情况,即当发生特定事件时,后端需要向Web应用程序发送一些数据/消息。)

I read about WebPush Notifications but I dont want to use them, because it asks the user for permission to show notifications, which is kinda wierd.

(我阅读了有关WebPush通知的信息,但我不想使用它们,因为它要求用户显示通知的权限,这有点奇怪。)

Even for using the WebPush API I have to request for permission to show notifications.

(即使使用WebPush API,我也必须请求显示通知的权限。)

And I dont want to rely on that because if the user clicks deny the whole application breaks which is why I was looking for other alternatives.

(我不想依靠它,因为如果用户单击“拒绝”,则整个应用程序都将中断,这就是为什么我要寻找其他选择的原因。)

What alternatives are available for me?

(有什么替代方法可供我选择?)

PS: The backend I'm using is built upon Django Rest Framework

(PS:我正在使用的后端基于Django Rest Framework构建)

  ask by Goutam B Seervi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One thing to consider is whether you want/need the user interface to show the changes real time .

(要考虑的一件事是是否要/需要用户界面实时显示更改。)

If showing your data is very time sensitive you probably want to consider a bi-directional protocol like websockets and receive the server updates through this channel.

(如果显示数据对时间非常敏感,则您可能需要考虑使用双向协议(例如websockets)并通过此通道接收服务器更新。)

Another way to mimic a near real-time UI update is to do polling .

(模拟近乎实时的UI更新的另一种方法是进行轮询 。)

The UI (on certain conditions) sets up a schedule to do periodic polling through HTTP using traditional APIs.

(UI(在特定条件下)设置时间表以使用传统API通过HTTP进行定期轮询。)

Polling can be a good option if the data requested can be served quickly (not an expensive computation and the amount of transferred data is small) and it does not require to re-render the entire client side of the application.

(如果可以快速提供所请求的数据(不是昂贵的计算,并且传输的数据量很小),并且不需要重新渲染应用程序的整个客户端,则轮询可能是一个不错的选择。)

Polling tends to be a bandaid solution and implemented as an afterthought therefore feel extremely heavy handed (lots of data transferred and re-rendering the UIs) but if you plan on light polling it could be a good option.

(轮询通常是一个临时的解决方案,并且是事后才想到的,因此感觉非常费力(传输大量数据并重新呈现UI),但是如果计划进行轻度轮询,那么这将是一个不错的选择。)

If you foresee more 'real-time' like aspects of your app, you may want to introduce websockets.

(如果您预见到应用程序的更多“实时”方面,则可能需要引入网络套接字。)

Be cautious on what this means to your architecture and scaling though.

(不过,请谨慎使用这对您的体系结构和扩展意味着什么。)

(There are certain architectural styles, eg microservices architectures, where using websockets may introduce more complexity).

((有某些架构样式,例如微服务架构,使用websocket可能会带来更多的复杂性)。)

Browser compatibility may also be a small concern so i'd make sure you are not planning to support browsers that do not support websockets.

(浏览器兼容性也可能是一个小问题,因此我将确保您不打算支持不支持websocket的浏览器。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...