REST API with active push notifications from server to client

你。 提交于 2020-11-30 06:27:21

问题


Problem description

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

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.

Possible solutions:

Synchronous API polling from client side

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

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

Advantages

  • fairly simple to implement

Problems

  • synchronous polling creates a huge amount of http requests

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

Websocket endpoint on the API

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

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

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.

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.

Advantages

  • API can push notifications to clients asynchronously

Problems

  • When a user terminates the websocket connection his dictionary entry will persis
  • Retaining one websocket connection per user permanently adds additional overhead to the API
  • Horizontal scalability of the API is more difficult because the service is not stateless anymore (Websocket connection information saved in

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.

Advantages

  • API remains stateless

Problems

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

  • Amount of queues grows drastically

  • Additional costs for RabbitMQ service

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

Final words

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.

best, D


回答1:


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

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.



来源:https://stackoverflow.com/questions/54224312/rest-api-with-active-push-notifications-from-server-to-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!