2
votes

I need to establish a long running (say 10 minutes) connection between a javascript client and a Web API controller action. The Azure load balancers for Web Apps have a default timeout of 4 minutes and that is not configurable at the moment.

How can I keep the connection by sending tcp keep-alive packets?

  • Can this be done client-side, server-side or both?

  • For .NET I can only find this option ServicePoint.SetTcpKeepAlive, but it seems to be limited to an HttpWebRequest where the application makes a request as a client.

  • Is it possible to send keep-alive packets from javascript via XMLHttpRequest?

1
can you just send a dummy message every so often? (your client need not respond to every utterance...)dandavis
Yes but that "pollutes" the response and requires extra client logic, no? I've looked into SingalR and long-polling. Might be the only solution atm.Benjamin E.
it depends on how you message the client, with something like signalR, each message has a type and only the type you bound listeners to will fire client actions, so no, there would not be extra logic on the client if you used an event emitter-shaped source. in terms of pollution, i think keeping it open is cleaner than reconnecting, and if the keep-alives are invisible to the program flow, i don't see the harm, Socket.IO and all the big dogs do this if it makes you feel better knowing that...dandavis

1 Answers

0
votes
var socketsHandler = new SocketsHttpHandler
{
      PooledConnectionIdleTimeout = TimeSpan.FromHours(27),//Actually 5 mins can be idle at maximum. Note that timeouts longer than the TCP timeout may be ignored if no keep-alive TCP message is set at the transport level.
      MaxConnectionsPerServer = 10
};
client = new HttpClient(socketsHandler);

As you can see, although I set the idle timeout to 27 hours, but actually it just keep 5 mins alive.

So, finally I just call the target endpoint using the same HttpClient every 1 min. In this case, there is always an established connection. You could use netstat to check that. It works fine.