How to implement asynchronous request response in REST based web service - web-services

I have a REST based web service system. I need to find a way to support publish/subscribe model here. As you know REST the communication between client and server is HTTP protocol. I use apache (PHP) web server in the backend to server all REST requests. The question is how to use PHP or whatever (in the web server side) to support this kind of Pub/Sub model. One typical scenario would be:
1) Client subscribes for a change in an object (GET /config/object/?type=async)
2) Client does not block with this request as it is async call.
3) Server accept the subscription and waits for the event.
4) Server publishes the client with the required data as and when the event happens.
I basically need to know how to implement all of these four steps above.

You are probably looking for something like PubSubHubbub -
http://code.google.com/apis/pubsubhubbub/
Letting PubSub implement the hub for you means you don't need blocking calls to the server.
They already have implemented example Subscribers and Publishers in different languages.

If haven't already, you should read Roy Fielding's take on the various approaches to PubSub. http://roy.gbiv.com/untangled/2008/paper-tigers-and-hidden-dragons

Related

Choosing the scenario of using Web Sockets in standard HTTP REST API

I will be happy to get advice from more experienced developers about adding Web Sockets into my HTTP-based project.
That’s the thing. I have developed the REST API based service. Everything works well enough, but… In some special cases my server needs a long time to serve client requests. It may be from 1 minute to several hours (and even days)! I implement some not-so-good algorithm to address this issue:
Client sends HTTP request
Server replies about registering request
Client starts sending HTTP requests to get necessary data (if response does not have needed information the client sends another request and so on)
That is all in a nutshell.
And it seems to be a bad scenario and I am trying to integrate web sockets for adding duplex-channels in this architecture. I hope that my API will be able to send info about updated data as soon as possible without the necessity of many requests from the client.
But I am a bit confused in choosing one of two ways to use web socket (WS).
Variant A.
The server only tells the client via WS that data is ready. And the client gets data by standard request-response HTTP method from REST API.
Variant B.
The server sends all data to the client via WS without HTTP at all.
What variant is more suitable? Or maybe some other variants?
I do not want to remove HTTP at all. I just try to implement WS for a particular kind of end-points.
Variant A would be more suitable and easy to implement. You can send message to the client after the data is ready, and he can then send request for the data. It will be like a simple chat websocket, and will serve your purpose.

web client call server, server call 3rd party, all async?

I have a website that is that is architected using an n-layer approach. The problem I have is that I need the client to make a call to the application layer, one of the other layers will then make a call to another web service somewhere in the world. The other web service could take some time to come back so what I would like to do, if possible, something along the lines of the following using async requests:
The client is HTML & JavaScript, the server layers are written in C# (.NET 4.5), the 3rd party web services are just web services I need to consume
How would you go about writing this?
Any help will be much appreciated
It doesn't look like the server-side code needs to call the external service asynchronously. I'm not seeing an advantage in doing that.
The client-side code would call the server-side code asynchronously, of course. (An AJAX request, using jQuery for example.) And it would await the response form that service in an asynchronous manner before handling that response. But since the server-side code in this request is only serving that one request, it can do so synchronously.
Indeed, if the server-side code were itself also asynchronous then it would return control to the client-side code immediately before it has anything useful to give it. Which means the client-side asynchronous handler can't do anything. Instead, when the server-side code gets a response from the external service, it would need to push that response to the client-side code. Which is possible with websockets and whatnot, but probably a lot more complex than this situation requires.
Only the first link in the chain needs to be asynchronous in order to provide the user experience of asynchronicity, the rest of the system doesn't need to be.
Whenever you have a "client/server" boundary, you have the option of making the server asynchronous as well as the option of making the client asynchronous. Those decisions are independent.
I'll make the assumption that your third-party webservice is scalable, or that your server has other things to do besides just this one request. In that case, I'd recommend that your server be asynchronous.
Asynchronous programming is natural with async/await in ASP.NET 4.5. Depending on your third-party service, you may want to use HttpClient or the async-compatible WCF or webservice proxies. Both ASP.NET MVC and WebAPI support asynchronous calls for your service.
On the client side, you have no choice; JavaScript in the browser must be asynchronous.

Is this service Restfull?

I am not sure if this is the correct place to ask but since there are a lot of questions of this kind i ll go ahead and ask.
A year ago i developed an iOS application that was connecting to a server with HTTP requests exchanging JSON files etc. I was told at that time that the server was a web (REST) service. I didnt care much since for me was just a black box.
The last months i am developing a hybrid mobile application where i use native code + jquery mobile for the front end part and php + mysql for the back end part. The application is about registering new users to the data base , having users to subscribe in various kinds of events , get notifications on them etc. So for all the communication between the front ent(client) and back end(server) i make http requests(POST) , ajax calls using json files.
Is this a RESTfull Web Service? I am a bit confused on the definition of REST. According to wikipedia REST is :
REST-style architectures conventionally consist of clients and servers. Clients initiate requests to servers; servers process requests and return appropriate responses.
So is what i am building a Restfull Web Service? Also do we call Rest only the server part or in general the client-server architecture?
Is it just a web Service? And if yes what kind of web service?
there is no difference when you invoke a controller (aka web service) from browser through http or from a client code (java, .NET or any language). when you want to invoke a service from outside world from your code.. you have choice of soap or rest .. soap is protocol, where as REST is an architecture style.. so REST is methodology of calling a service (which is simple http POST or GET call) from outside world .. not necessarily from a browser .. it may from client code which acts as browser to get response from outside world service..this is based on my experience..

two ways communication in web services

How can I have two ways communication with WS. Two ways means a client could be a server and a server could be a client. As far as I understand the problem related to the client-server model in HTTP which is used by WS. What is the best practice for this scenario when a server wants send an event to multiple clients without being polled. ?
As far as I know there are some solutions but I do not know which one is best
1) server-push techniques (websockets)
2) SOAP over JMS (this sounds great)
3) WS-eventing
Thanks
The purpose of a webservice is - as the name says - to serve. It responds to requests, but it never sends requests on its own (but an application accessed through a webservice interface could send requests to other webservices to fulfill a request sent to it).
When a component of a service-oriented architecture is supposed to receive events from another component, it means that the receiving component has to act as a server and expose an own web services interface so that the component where the event occured can call it.

Notify server to client via Web service

I made a WebService chat. At the client side I am running a thread to check periodically if there any new messages available.
I want to know is there are any way to notify clients via Web Service.
I found something call 'Solicit Response' related to web service. But I am not aware how it works.
Any help is appreciated.
Thank you
In web services, the clients strictly request, they don't listen. What you need is some way for the server to "push" to the client. You're looking for something like Comet which isn't part of a web service per se.
Edit
Here's a relevant stackoverflow discussion.