Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server sends "ping request" to client in a single URL using JAX-RS and RESTeasy

I would like to create a web application that is able to "ping" the client once the client has accessed certain URL (e.g. www.example.com/ping/hello) in order to get the round trip time between server and client. And by "ping" request i mean a simple request with a timestamp from server and client sends back response with its timestamp. I was hoping for this activity to be done with a single URL if possible.

The flow is something like this:

  1. Client goes to the URL
  2. Server sends the response to the client with its timestamp
  3. Client then sends another response to server with new timestamp
  4. Server finally concludes the connection with 200 OK

So far I've only been able to do the first and second steps but not sure how to ensure client to go to the same URL again without back to the first step.

My server code is something like this:

@GET
@Path("/helloping")
public Response getPingServerClient(@Context HttpServletRequest req) {
    String result = Long.toString(System.currentTimeMillis());

    return Response.status(200).entity(result).build(); 

    //the code to receive the response from client containing timestamp
}

Is there a way to do that?

like image 497
Ihsan Haikal Avatar asked Aug 31 '18 15:08

Ihsan Haikal


2 Answers

I'm not a fan of what you are proposing because you're basically forcing the client to setup up code to effectively become a server, itself. This is inconvenient for the client.

Instead, consider a ping-pong approach where the client first calls the server's ping endpoint, which returns the server's timestamp. As soon as the client obtains the server's ping response, the client is instructed to call a second pong method, which accepts the new timestamp.

It's easier and simpler to require the client to call web service methods than it is to force to client to become a pseudo server. Hence the recommendation.

like image 146
entpnerd Avatar answered Nov 18 '22 06:11

entpnerd


There are two client to server calls. You'll have to figure out a way to differentiate between these two calls.

I can think of 3 options for this purpose:

  1. HTTP header
  2. Query parameter in GET request
  3. POST request with a marker to differentiate the two calls

The request/response flow will be something like this:

Client -> Server : Request
Server -> Client : Response with timestamp t1 
Client -> Server : Request with timestamp t2 and the above mentioned marker
Server -> Client : Response 200

In this approach, you'll have to write custom code at both server and client side to handle the mentioned logic.

like image 26
Aditya Abhas Avatar answered Nov 18 '22 04:11

Aditya Abhas