title: Websocket connection being closed on Google Compute Engine
body: <p>I have a set of apps deployed in Docker containers that use websockets to communicate. One is the backend and one is the frontend.</p>
  <p>I have both VM instances inside instance groups and served up through load balancers so that I can host them at https domains.</p>
  <p>The problem I'm having is that in Google Compute Engine, the websocket connection is being closed after 30 seconds.</p>
  <p>When running locally, the websockets do not time out. I've searched the issue and found these possible reasons, but I can't find a solution:</p>
  <ol>
  <li><p>Websockets might time out on their own if you don't pass &quot;keep alive&quot; messages to keep them active. So I pass a keep alive from the frontend to the backend, and have the backend respond to the frontend, every 10 seconds.</p>
  </li>
  <li><p>According to the <a href="https://cloud.google.com/load-balancing/docs/https/#websocket_proxy_support" rel="nofollow noreferrer">GCE websocket support docs</a>, some type of &quot;upgrade&quot; handshake needs to occur between the backend and the frontend for the websocket connection to be kept alive. But <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism" rel="nofollow noreferrer">according to MDN</a>, &quot;if you're opening a new connection using the WebSocket API, or any library that does WebSockets, most or all of this is done for you.&quot; I am using that API, and indeed when I inspect the headers, I see those fields:</p>
  </li>
  </ol>
  <p><a href="https://i.stack.imgur.com/6V9EO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6V9EO.png" alt="websocket connection headers" /></a></p>
  <ol start="3">
  <li>The <a href="https://cloud.google.com/load-balancing/docs/backend-service#session_affinity" rel="nofollow noreferrer">GCE background service timeout docs</a> say:</li>
  </ol>
  <blockquote>
  <p>For external HTTP(S) load balancers and internal HTTP(S) load
  balancers, if the HTTP connection is upgraded to a WebSocket, the
  backend service timeout defines the maximum amount of time that a
  WebSocket can be open, whether idle or not.</p>
  </blockquote>
  <p>This seems to be in conflict with the <a href="https://cloud.google.com/load-balancing/docs/https/#websocket_proxy_support" rel="nofollow noreferrer">GCE websocket support docs</a> that say:</p>
  <blockquote>
  <p>When the load balancer recognizes a WebSocket Upgrade request from an
  HTTP(S) client followed by a successful Upgrade response from the
  backend instance, the load balancer proxies bidirectional traffic for
  the duration of the current connection.</p>
  </blockquote>
  <p>Which is it? I want to keep sockets open once they're established, but requests to initialize websocket connections should still time out if they take longer than 30 seconds. And I don't want to allow other standard REST calls to block forever, either.</p>
  <p>What should I do? Do I have to set the timeout on the backend service to forever, and deal with the fact that other non-websocket REST calls may be susceptible to never timing out? Or is there a better way?</p>