How to deploy flask-socketio app to Google Cloud Platform App Engine

1.1k Views Asked by At

I coded a flask-socketio app that serves data to a website via websockets.

My directory structure is like this:

  • main.py
  • website
    • index.html
    • main.js
    • style.css

My app.yaml is like this:

runtime: python38

handlers:
  - url: /
    static_files: website/index.html
    upload: website/index.html

  - url: /
    static_dir: website

entrypoint: python main.py

I ran gcloud app deploy in the directory and managed to deploy the website to GCP. It is now available under https://codevis.ew.r.appspot.com/

However, the flask-socketio app isn´t running and I can´t connect to it with the website.

My main.py deploys the flask-socketio app like this:

app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")

[...]

def run_app():
    socketio.run(app, debug=True)


if __name__ == "__main__":
    run_app()

In my main.js, I am trying to connect to flask-socketio like this:

var socket = io.connect("https://codevis.ew.r.appspot.com:5000");

When testing the flask-socketio locally (using var socket = io.connect("http://127.0.0.1:5000");), everything works perfectly. I just can´t get GCP App Engine to actually run the flask-socketio app. What am I doing wrong?

2

There are 2 best solutions below

0
On BEST ANSWER

App engine standard doesn't support websockets. You can do it on App Engine Flexible, but you can't scale to 0 (and that implies extra cost)

For a scale to 0 platform, you can consider Cloud Run which will be able to accept be directional websockets soon.

0
On

I came across a Feature Request existing for the implementation of this feature.

You may star it so that it gains visibility and also include your email in the CC section in order to receive further updates on this. However, please keep in mind that as it is a Feature Request there is no ETA available for the implementation at this time.