Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 715
chrpinedo Avatar asked May 19 '16 09:05

chrpinedo


People also ask

What are the required components of a REST API request?

Any REST request includes four essential parts: an HTTP method, an endpoint, headers, and a body. An HTTP method describes what is to be done with a resource.

How do I create a pub/sub API?

Create a new project in the Cloud Platform Console. In the left pane of the console, select Pub/Sub and then select Enable API.


1 Answers

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.

like image 169
bryanmac Avatar answered Oct 18 '22 22:10

bryanmac