REST API具有从服务器到客户端的活动推送通知 [英] REST API with active push notifications from server to client

查看:68
本文介绍了REST API具有从服务器到客户端的活动推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin应用程序,该应用程序使用以Python烧瓶编写的REST API.

i am working on a Xamarin application that consumes a REST API written in Python flask.

Xamarin应用程序提供虚拟购物清单,用户可以在其中进行协作以购买他们在共享清单上拥有的东西.

The Xamarin application offers virtual shopping lists where user can collaborate on buying stuff they have on a shared list.

为了改善用户体验,我希望能够主动通知用户列表中的成品.

To improve the user experience, i want to be able to actively notify the user about finished items on the list.

通知由API存储在关系数据库中,并具有一个标志,指示用户是否已经收到通知.

Notifications are stored by the API in a relational database and have a flag indicating if the user received the notification already.

API具有一个端点 GET/users/:user_id/notifications/,该端点在数据库中查询通知并返回带有这些通知的JSON响应.

The API has an endpoint GET /users/:user_id/notifications/ that queries the database for notifications and returns a JSON response with those.

  • 实施起来很简单
  • 同步轮询会创建大量的http请求

  • synchronous polling creates a huge amount of http requests

API服务保持无状态,从而使使用负载均衡器的水平扩展更加容易

API service remains stateless, making a horizontal scaling with a loadbalancer easier

API具有端点 POST/users/:user_id/notifications/register ,该端点在客户端和API之间创建Websocket连接.

The API has an endpoint POST /users/:user_id/notifications/register which creates a websocket connection between client and API.

该连接存储在一个全局数组中,其中每个条目都将一个客户端ID映射到一个websocket连接.

The connection is stored to a global array in which each entry maps a client id to a websocket connection.

创建新通知时,端点通过将通知的所有者ID与字典条目进行比较,在连接字典中进行查找.该通知通过网络套接字发送给适当的用户.

When a new notification is created, the endpoint makes a lookup in the connection dictionary by comparing the owner id of the notification with the dictionary entries. The notification is sent to appropriate user through the websocket.

通知与第一种方法一样存储在数据库中.

Notifications are stored in the database like in the first approach.

当用户调用端点时,将首先建立一个新的Websocket连接,一旦成功,API将把所有看不见的通知从数据库发送给用户.

When a user calls the endpoint, a new websocket connection will be established first and upon success the API sends all unseen notifications from the database to the user.

  • API可以异步将通知推送给客户端
  • 当用户终止websocket连接时,他的词典条目将持续存在
  • 为每个用户保留一个websocket连接会永久增加API的额外开销
  • API的水平可伸缩性更加困难,因为该服务不再不是无状态的(Websocket连接信息保存在

API使用RabbitMQ服务将通知发送到客户端.每个客户端都使用订阅来订阅自己的通知队列,以防止消息广播.

The API uses a RabbitMQ service to send notifications to the client. Every client uses subscribes to his own notification queue to prevent the broadcasting of messages.

  • API保持无状态
  • 用户离线时需要将通知重新发送到交换处

  • Notifications needs to be resend to the exchange when a user is offline

队列数量急剧增加

RabbitMQ服务的额外费用

Additional costs for RabbitMQ service

当许多用户同时联机时,RabbitMQ服务的临时负载较高

High temporary load on the RabbitMQ service when many users come online in the same time

听到别人的意见会很有趣.

It would be interesting to hear the opinion of others.

我认为在一个非常常见的用例中,从后端服务到客户端的通知主动分发.

I believe the active distribution of notifications from backen services to clients i a very common use case.

最好,D

推荐答案

我可以推荐另一种提供JSON的API的方法,该方法称为

I can recommend on a different approach for API that provides JSON which is called GraphQL

它支持GraphQL API服务器(使用Web套接字)推送的订阅功能
如今,GraphQL被认为比RESTful API更好,因为它非常灵活,您只需一次查询就可以准确获取所需的数据.

It supports subscriptions capabilities that are pushed by the GraphQL API Server (using web sockets)
GraphQL is considered today to be better than RESTful API since its very flexible and you can get exactly the data you need with one query.

这篇关于REST API具有从服务器到客户端的活动推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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