Is it Ok to use HTTP REST API for Chat application?

后端 未结 5 598
眼角桃花
眼角桃花 2020-12-23 17:24

We are building a chat application in Android. We are thinking of using HTTP REST API to send outbound messages. Wanted to know if its a good approach or has any downsides c

5条回答
  •  遥遥无期
    2020-12-23 18:16

    I think a REST approach can work for chat. Let's assume:

    • http://chat.example.com/conversations references all conversations
      • GET fetches the list
      • POST creates a new one
    • http://chat.example.com/conversations/123 references conversation #123
      • GET fetches messages in this conversation
      • POST adds a message in the conversation

    If I understand correctly, your question is about the last point.

    Once the client has posted an outboud message to http://chat.example.com/conversations/123, it will close the http connection.

    The drawback is that receiving inbound messages is simply not possible in that case. You will need a different channel (maybe simply Google cloud messaging).

    On the contrary, WebSockets and XMPP keep the connection alive, hence they can receive messages with no delay. But the drawback of both of them is indeed that this represents a cost on the server, in terms of scalability ; and a cost on the client in terms of battery usage.

    On the server:

    • sockets are a relative scarce resource
    • it's not possible to move clients for load balancing if they have an open connection (but can you really move clients anyway? this depends on the responsibility of tiers)

    On the client:

    • maintaining a network connection is extremely expensive. 1 s network ≈ 5 min sleep.
    • the XMPP librairies are not necessarily working very well on Android. And I've no idea of support of WebSockets on android.

提交回复
热议问题