2

I am working on an API application on Rails 4.2.0. Wonder if there any way I could tell if the client closes the connection after they made API call to the server.

Tried session and cookie, seems they do not design for it.

Mao
  • 23
  • 2
  • What do you mean by "closed the connection" ? Isn't the user just making regular HTTP requests? – lacostenycoder Sep 05 '19 at 21:21
  • for example, people hard-reload page and close the window. The request has been submitted to the server and line up to wait for execute at server side. After quest finished, server-side will return 200 but meanwhile people already walk away, there is no receiver anymore. – Mao Sep 06 '19 at 14:01

2 Answers2

2

Every HTTP requests ends up being closed when it's complete, as that's how the HTTP request-response cycle works. It's extremely rare to have connections hanging open as long-polling fell out of style once WebSocket was standardized.

After the client has made a call you can assume that request has completed and they've disconnected. There's no way of knowing if they will make additional requests or not, it's entirely up to the client.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

In response to your comment on what you are actually trying to do, the only way I can think of would be via javascript using onunload. Here is a very basic example.

<body onunload="handleOnClose()">
  <script>
  function handleOnClose()
  {
      // send something to your backend 
  }
  </script>
</body>

For more info see this SO question

lacostenycoder
  • 10,623
  • 4
  • 31
  • 48