How to detect disconnection for POST request in JSON RestFul API Service PHOENIX?

I need detect a client disconnection in the server side, for client call POST resquest and try to RollBack Apply.

In this moment my PHOENIX RestFul Server api don’t detect the client disconnection .

The happy path flow for POST call:

                         Request

1 .- Client -------------------> Server EndPoint
2.- tasks
Response
3.- Client <--------------------render(conn, “payload.json”, result)

My flow problem :

              Request

1 .- Client ------------------> Server EndPoint
2.- tasks
3.- Client Disconnection .
Response
-----/////------------- render(conn, “payload.json”, result)
(“My app don’t detect client disconnection”)

How to detect the client’s disconnection’s ???

Context:
I made a Service RestFul in the way :

https://medium.com/@pamit/building-a-restful-backend-with-elixir-phoenix-84fe390975c

Router:

use BusiApiWeb, :router

   pipeline :api do
    plug :accepts, ["json"]
  end

  pipeline :auth do
    plug BusiApiWeb.Auth.Pipeline
  end

  scope "/api", BusiApiWeb do
    pipe_through :api
    post "/users/signup", UserController, :create
    post "/users/signin", UserController, :signin
  end
 scope "/api", BusiApiWeb do
    pipe_through [:api, :auth]
    post "/payload",PayloadController, :payload
  end

Controller:

....
   use BusiApiWeb, :controller
       def payload(conn, _params) do   
         render(conn, "payload.json")
     end

View :

....
use BusiApiWeb, :view

def render("payload.json") do
    %{ tag:   "ok" }
end

I’d discourage you from even trying this. What are you trying to accomplish and why?

OK, now that’s in your head, imagine that the client crashes after you’ve sent the response and it’s reading the data. Or that the client reads the whole response, then crashes parsing it, or…

If you absolutely have to rollback if the client doesn’t display confirmation, then you need to have the client POST an acknowledgement.

[EDIT] And even then, you don’t have a way to know if the response is rendered and displayed, followed by a crash before the acknowledgement POST goes out, or that the ack POST is sent but never arrives. So you have the possibility of rolling back unnecessarily.

2 Likes

Thank’s for your advice, that is for responsibility.

OK, now that’s in your head, imagine that the client crashes after you’ve sent the response and it’s reading the data. Or that the client reads the whole response, then crashes parsing it, or…

That is client responsibility, My server restFull responsibility end when the client received the response.

For example in a client-server socket , the server or client knows if the socket descriptor has been loss, before they try to send a message to the other side.

As they (client/json api) are not even connected (html is stateless), it’s hard to detect disconnection…