Flask-socketio: heroku issues

Created on 1 Dec 2015  Â·  32Comments  Â·  Source: miguelgrinberg/Flask-SocketIO

Miguel I've tried launching your example on folder and uploaded it up to heroku.
here https://test-socketz.herokuapp.com/

I changed this line 13 on the index.html

var socket = io.connect('https://' + document.domain + ':' + location.port + namespace);
from http to https since heroku complains if it's only on http

besides that I did not change anything.
the app will keep disconnecting and reconnecting and clicking echo will sometimes work and sometimes not work.

question

All 32 comments

@halcyonjuly7 try using gunicorn 18.0 instead of the latest 19.3. There is a suspected issue on the 19.x releases that prevents WebSocket connections from working.

Reference: https://github.com/benoitc/gunicorn/issues/1147

Gunicorn version is 18.0.0 checking the dev tools console
reveals errors it mentions

https://test-socketz.herokuapp.com/socket.io/?EIO=3&transport=polling&t=1449003680313-7&sid=34b72b8581904686ae7d79c448eb02d7 400 (BAD REQUEST)

https://test-socketz.herokuapp.com/socket.io/?EIO=3&transport=polling&t=1449003648103-5&sid=34b72b8581904686ae7d79c448eb02d7 Failed to load resource: the server responded with a status of 400 (BAD REQUEST)

is this a heroku issue?

On Dec 1, 2015 1:28 PM, "Miguel Grinberg" [email protected] wrote:

@halcyonjuly7 https://github.com/halcyonjuly7 try using gunicorn 18.0
instead of the latest 19.3. There is a suspected issue on the 19.x releases
that prevents WebSocket connections from working.

—
Reply to this email directly or view it on GitHub
https://github.com/miguelgrinberg/Flask-SocketIO/issues/176#issuecomment-161070376
.

can you share the requirements.txt file that you are using and the output of heroku logs?

requirements.txt

``` amqp==1.4.7
anyjson==0.3.3
beautifulsoup4==4.4.1
billiard==3.3.0.21
blinker==1.4
celery==3.1.19
dominate==2.1.16
eventlet==0.17.4
Flask==0.10.1
Flask-Admin==1.3.0
Flask-Bootstrap==3.3.5.6
Flask-Login==0.3.1
Flask-Mail==0.9.1
flask-mongoengine==0.7.1
Flask-Principal==0.4.0
Flask-PyMongo==0.3.1
Flask-Security==1.7.4
Flask-SocketIO==1.1
Flask-SQLAlchemy==2.0
Flask-WTF==0.12
greenlet==0.4.9
gunicorn==18.0.0
itsdangerous==0.24
Jinja2==2.8
kombu==3.0.29
MarkupSafe==0.23
mongoengine==0.10.0
passlib==1.6.5
pep8==1.6.2
pymongo==2.9
python-engineio==0.8.0
python-socketio==0.7.0
pytz==2015.7
redis==2.10.5
requests==2.8.0
six==1.10.0
SQLAlchemy==1.0.8
Werkzeug==0.10.4
wheel==0.24.0
WTForms==2.0.2


heroku logs

2015-12-01T21:50:09.275560+00:00 app[web.1]: async_mode is eventlet
2015-12-01T21:50:09.486823+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=1449006572221-43&sid=ebf94e94a44442e7a92d7dcfc6083108" host=test-socketz.herokuapp.com request_id=96bc710a-984e-48ec-aa3b-da014abc8260 fwd="75.88.134.232" dyno=web.1 connect=3ms service=1693ms status=400 bytes=254
2015-12-01T21:50:11.386598+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=1449006605868-44" host=test-socketz.herokuapp.com request_id=2bbc0abd-00bb-45b9-b3b1-a8826e6b0138 fwd="75.88.134.232" dyno=web.1 connect=1ms service=17ms status=200 bytes=417
2015-12-01T21:50:11.540252+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&t=1449006605956-46&sid=9e03e3138a1a44a8823ce2788a41fb78" host=test-socketz.herokuapp.com request_id=85be1cb4-ff25-4cd2-b14a-93087521b5f3 fwd="75.88.134.232" dyno=web.1 connect=1ms service=5ms status=200 bytes=317
2015-12-01T21:50:11.466206+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&t=1449006605956-45&sid=9e03e3138a1a44a8823ce2788a41fb78" host=test-socketz.herokuapp.com request_id=b271b6b0-174b-49ce-88d8-0960bca18c51 fwd="75.88.134.232" dyno=web.1 connect=2ms service=3ms status=200 bytes=269
```

One more request. Can I see the Procfile?

no problem miguel ask what you need to because this has really bugged me

procfile

web: gunicorn app:app

the app.py file is the same app.py file here https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/example/app.py

Okay, good. Please change your procfile to:

web: gunicorn -k eventlet app:app

Let me know if this helps. The way you had it, you weren't enabling eventlet, which is required by WebSocket.

it's working now. nice.

maybe that could be documented for future inquiries with regards to heroku and socketio

I couldn't find that anywhere.

but thank you. it works now

I was under the impression that since running it locally eventlet was run automatically by flask-socketio it would it would have been same the case too. but ok

thank you for the time

am getting this on my heroku
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

@virgincodes run "heroku logs" to see what's going on. Your application is not starting properly.

done that its telling me no process running

Sorry, can't really help you with the little information you are providing. The application isn't starting, you need to figure out why.

tanks @miguelgrinberg i was able to fix it i forgot to put web: prefix and when it ran no process was recognized am sorry for the trouble and tanks

@halcyonjuly7 try using gunicorn 18.0 instead of the latest 19.3. There is a suspected issue on the 19.x releases that prevents WebSocket connections from working.

Reference: benoitc/gunicorn#1147

same issue here
downgrade from 19.9 to 18.0 make my app work
thank you so much Miguel

Hi Miguel!

I have some trouble deploying my app.

I'm getting _Application error_
with Procfile web: gunicorn -k eventlet application:app

or _400 BAD RESPONSE in console_
with Procfile web: gunicorn app:app

_requirements.txt_
Flask==0.10.1
Flask-Session==0.3.1
Flask-SocketIO==1.1
gunicorn==18.0.0
(downgraded Gunicorn from 20.0.4 to 18 and Flask_SocketIO from 4.2.1)

_heroku logs_
2020-03-18T22:32:23.713716+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/socket.io/?EIO=3&transport=polling&t=1584570743108-381" host=guarded-river-17904.herokuapp.com request_id=9f874a55-d954-4c00-8bc9-02cbe4db25a9 fwd="181.230.79.165" dyno= connect= service= status=503 bytes= protocol=https

Thanks for any help.
Mariano

The command is web: gunicorn -k eventlet -w 1 application:app, you have to run a single worker. Try that and get logs again if you get errors.

It worked, but I had to add some libraries also. Thanks!!!

requirements.txt
Flask==1.1.1
Flask-Session==0.3.1
Flask-SocketIO==4.2.1
gunicorn==18.0.0
eventlet==0.25.1
requests==2.23.0
Werkzeug==0.16.0

@miguelgrinberg Is it possible to deploy it to heroku without gunicorn and just use eventlet? In the docs, it says "The simplest deployment strategy is to have eventlet or gevent installed, and start the web server by calling socketio.run(app) as shown in examples above. This will run the application on the eventlet or gevent web servers, whichever is installed."

My Procfile is simply web: python app.py but it is still giving me the same R10 error (web process failed to bind). My requirements.txt file has eventlet in it, and I have a runtime.txt file with python-3.7.5 in it.

thank you so much!

@adshastri you can deploy any web server to Heroku, but you have to configure it on the port indicated by the $PORT environment variable. Gunicorn does this automatically, eventlet does not.

@miguelgrinberg yup, i did that. this is my code:

if (os.environ.get('PORT')):
         port = os.environ.get('PORT')
else:
         port = 5000

socketio.run(app, port=port, debug=True)

I still got the R10 error of failure to bind. to be clear, i do see a port other than 5000 in the logs so that code does seem to be working. i really appreciate you helping by the way.

Did you look at your heroku logs? Something must be not working there. Not sure if this is the problem, but normally one passes an integer for port. You are passing a string.

Converting the port to an integer didn't seem to make any difference. The logs do not say anything relevant:

2020-05-22T17:13:04.624250+00:00 heroku[web.1]: Starting process with command `python app.py`
2020-05-22T17:13:08.723612+00:00 app[web.1]:  * Restarting with stat
2020-05-22T17:13:09.337425+00:00 app[web.1]:  * Debugger is active!
2020-05-22T17:13:09.365077+00:00 app[web.1]:  * Debugger PIN: 258-991-099
2020-05-22T17:13:09.373403+00:00 app[web.1]: (8) wsgi starting up on http://127.0.0.1:13319
2020-05-22T17:14:05.057778+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2020-05-22T17:14:05.076621+00:00 heroku[web.1]: Stopping process with SIGKILL
2020-05-22T17:14:05.174573+00:00 heroku[web.1]: Process exited with status 137
2020-05-22T17:14:05.219419+00:00 heroku[web.1]: State changed from starting to crashed

I hope I'm not bothering you with anything but it seems to be something specific to flask-socketio. I stripped out everything from my app.py (socketio handlers, imports, etc.) and simply replaced it with the following bare bones code:

import os
from flask import Flask, request
from flask_socketio import SocketIO, join_room, leave_room, send, emit
app = Flask(__name__, static_folder="./client/build", static_url_path="/")

socketio = SocketIO()
socketio.init_app(app)

@app.route('/')
def index():
    return app.send_static_file('index.html')

if __name__ == '__main__':
    if (os.environ.get('PORT')):
        port = int(os.environ.get('PORT'))
    else:
        port = 5000
    socketio.run(app, port=port, debug=True)

I still get the H10 error, but it's working locally. Maybe the flask app, under the hood, isn't being passed the appropriate port?

They actually do. You are listening on localhost. I believe that will prevent any access from outside the container in which your app runs. Set host to 0.0.0.0. 🤞

Ah thanks, that seemed to get it to run at least.

I'm following your tutorial here: https://blog.miguelgrinberg.com/post/how-to-deploy-a-react--flask-project.

It works locally now, but I'm getting a 404 for the "/" route on Heroku. If I take out the return app.send_static_file('index.html') and replace it with return "random", it works properly. Any idea why this is happening? Everything else is as you specify in the guide, except I'm using Flask-SocketIO obviously.

@adshastri I assume you have an index.html file in your static directory? That's the only way the send_static_file is going to work.

Oh, in the guide that I linked, you left the index.html in the build folder of the react project and pointed the static_folder parameter of the Flask constructor to that build folder. How else can I render the file when the user visits "/"?

Yes, that's fine. By static folder I meant the folder that is configured as the static folder in the Flask app.

So let me ask again. Do you have an index.html file in your build folder? I mean in Heroku, not in your local system.

Looks like npm's default .gitignore included the build folder so the index.html wasn't getting pushed. I really appreciate all the help @miguelgrinberg :+1:

@miguelgrinberg .
Hey there.
My issue is somewhat similar to the starting point of this issue. While deploying my app to heroku server, I can't seem to get socket io to work at all.
web: gunicorn -k eventlet -w 1 app:app
I entered this in my procfile yet it's still not even recognizing the .connect event of socketio. I have both eventlet and gunicorn installed in my requirements.txt file, yet the background task i want to run isnt getting executed on the server. Pls help

hey sorry to have bothered you.
I sorted out the issue. All I had to do was downgrade my gunicorn to 18.0 version and that solved the problem!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fbussv picture fbussv  Â·  4Comments

EndenDragon picture EndenDragon  Â·  3Comments

blstdmi picture blstdmi  Â·  3Comments

dlernz picture dlernz  Â·  4Comments

hrmon picture hrmon  Â·  5Comments