Long-polling (user notification) with a RESTful architecture

前端 未结 2 741
忘掉有多难
忘掉有多难 2021-01-16 13:51

I\'m working on a simple RESTful api (on NodeJs). I understand that being restful implies that horizontal scaling will be much easier. Unfortunately, I need some way for cli

2条回答
  •  有刺的猬
    2021-01-16 14:02

    Long polling approach puts the burden on client to make periodic requests - hence they are separate requests. There is not "notifying the client" with Long polling. The client polls.

    You can use distributed cache to store the intermediate state information to which both A and B will have access to. Thus Server A and B can take part in the conversation with the client because it is all kept in that distributed cache.

    Client Alpha Request 1 -> Node A =) Cache it as k,v (Aplha, State<-has request info)
    

    Meanwhile one or more Node A,B,C or D works on it and takes its own time.

    Client Alpha Request 2 -> Node B =) Retrieve Cache it as k,v (Aplha,State <-not done)
    (are we done yet?)
    

    Meanwhile one Work its Done. Your Request is fulfilled :) i.e.

    Node X =) Update cache (Aplha, State <- results)

    Client Alpha Request 2 -> Node B =) Retrieve Cache it as k,v (Aplha, State<-results)
        (are we done yet?)                      (here you Sir - your results are now ready)
    

    WebSockets: Another options is to go for WebSockets instead of Long Polling

        Connection between Client and Node A is persistent with bidirectional communication.
    

提交回复
热议问题