Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websockets not working on Google App Engine

I recently deployed a node.js server application to Google App Engine that communicates with a client application via socket.io. Accordingly to this article, websockets are now supported on App Engine.

However, the client app is unable to connect to the server over the wss protocol. I can see the following error message in the browser console. (I removed my server domain)

WebSocket connection to 'wss://[my server]/socket.io/?EIO=3&transport=websocket&sid=dbB2UgsCYhD7c1ucAAAA' failed: Error during WebSocket handshake: Unexpected response code: 400

Socket.io then falls back to https long polling, which works well.

Here is my app.yaml for deployment to app engine, with session_affinity set to true for long polling.

runtime: nodejs10

instance_class: F2

handlers:
- url: /.*
  secure: always
  redirect_http_response_code: 301
  script: auto

network:
  session_affinity: true

Socket.io is used on the server in a straightforward way:

this.server = this.app.listen(this.PORT, (err: any) => {
  io = socketio().listen(this.server);
  io.on("connection", (socket) => {
    console.log('A socket connection was made!');
  });
});

I am wondering how to get the websocket connection to work on App Engine? Maybe a firewall rule or other configuration change is needed?

like image 577
wooters Avatar asked Sep 05 '19 02:09

wooters


People also ask

Does Google App Engine support WebSockets?

You can use WebSockets to create a persistent connection from a client (such as a mobile device or a computer) to an App Engine instance. The open connection allows two-way data exchange between the client and the server at any time, resulting in lower latency and better use of resources.

How do I fix WebSocket connection error?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

How do I enable WebSockets?

- In Control Panel, click Programs and Features, and then click Turn Windows features on or off. Expand Internet Information Services, expand World Wide Web Services, expand Application Development Features, and then select WebSocket Protocol. Click OK. Click Close.


1 Answers

var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);

const forceSecure = (req, res, next) => {
  if (req.secure)
     return next(); // https -- Continue

  res.redirect('https://' + req.hostname + req.url)
}

/**
* This will force
* ALL HTTP requests ( GET, POST, OPTIONS, etc. )
* on ALL route handlers to be
* redirected to https 
*/
app.all('*', forceSecure);

app.get('/', (req, res, next) => {
  console.log('example');
  res.end();
});

app.ws('/', (ws, req) => {
  ws.on('message', msg => {
    console.log(msg);
  });
  console.log('socket', req.example);
});

app.listen(3000);

As of now, WebSockets are only supported in the Flexible Environment for App Engine. From what I see, you are deploying your application in the Standard Environment, as denoted by the instance_class you specified: instance_class: F2. Thus, I would suggest changing the environment from Standard to Flexible, see here for the app.yaml Configuration File in Flex. Doing so, will allow you to take advantage of the WebSockets functionality recently implemented in App Engine.

like image 108
JKleinne Avatar answered Nov 15 '22 07:11

JKleinne