Mao
Mao

Reputation: 23

How could detect if client close the connection on rails 4.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.

Upvotes: 2

Views: 265

Answers (2)

lacostenycoder
lacostenycoder

Reputation: 11226

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

Upvotes: 0

tadman
tadman

Reputation: 211670

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.

Upvotes: 2

Related Questions