Designing a REST API with req/resp and pub/sub requirements

chrpinedo picture chrpinedo · May 19, 2016 · Viewed 10.3k times · Source

Nowadays I'm designing a REST interface for a distributed system. It is a client/sever architecture but with two message exchange patterns:

  • req/resp: the most RESTful approach, it would be a CRUD interface to access/create/modify/delete objects in the server.
  • pub/subs: this is my main doubt. I need the server to send asynchronous notifications to the client as soon as possible.

Searching in the web I found that one solution could be to implement REST-servers in the server and client: Publish/subscribe REST-HTTP Simple Protocol web services architecture?

Another alternative would be to implement blocking-REST and so the client doesn't need to listen in a specific port: Using blocking REST requests to implement publish/subscribe

I would like to know which options would you consider to implement an interface like this one. Thanks!

Answer

bryanmac picture bryanmac · Aug 28, 2018

Web Sockets can provide a channel for the service to update web clients live. There's other techniques like http long polling where the client makes a "blocking" request (as you referred to it) where the service holds the request for a period of less than a timeout (say 50 sec) and writes a response when it has data. The web client immediately issues another request. This loop creates a continuous channel where messages can be "sent" from the server to the client but it's initiated from the client (firewalls, proxies, etc...)

There are libraries such as socket.io, signalR and many others that wrap this logic and even fallback from websockets to long polling gracefully for you and abstract away the details.

I would recommend write some sample web socket and long polling examples just to understand but then rely on libraries like mentioned above to get it right.