Flask-socketio: Failed to load resource: the server responded with a status of 400 (BAD REQUEST)

Created on 20 Sep 2017  路  9Comments  路  Source: miguelgrinberg/Flask-SocketIO

Hello

I am having an issue with Socket.io that is driving me nuts. I have just implemented it but I keep getting a 400 error.

Project Details:
I am running a Flask App on an AWS EC2 server and I am installing Flask-SocketIO to get live data from a raspberry Pi. The raspberry pi data is sent securely over the internet and the server is supposed to display the data live on a dashboard build by bootstrap and flask. I am using SocketIO to achieve this live data portion of this project.

I have been following this guide(https://github.com/vjammar/aws-iot-python) to try and get my project to work.

In the console for the web page I get this error:
Failed to load resource: the server responded with a status of 400 (BAD REQUEST)

In my apache2 error logs I get this:
[Wed Sep 20 17:51:23.182416 2017] [wsgi:error] [pid 24191:tid 140437234870016] WARNING:engineio:Invalid session (*Long Session ID *)

I have tried to get the engineio logs to output to the console with no luck.

I will provide any information / files you need!

Any help is appreciated!

question

All 9 comments

Have a look at the deployment section of the Documentation to learn about the possible configurations. Apache/mod_wsgi is sadly not a great choice for Socket.IO applications as it does not support async.

What would you recommend as the best option to be? I only have experience with apache as of right now.

Nginx + gunicorn + eventlet is a safe choice that is not hard to configure.

Hi Miguel

I have followed your advice and migrated everything to nginx + gunicorn + eventlet. However, I am having a new issue... Whenever I access the page I am going to be using socket.io features on I get a 502 Bad Gateway error. I have used everything from your documentation (nginx setup, gunicorn startup command) but I am still having no luck.

Here is my Nginx setup currently:

server {
listen 80;

server_name _;

location / {
    include proxy_params;
    proxy_pass http://127.0.0.1:8000;
    #proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Host $proxy_add_x_forwarded_for;
    proxy_redirect default;

}

location /socket.io {
    include proxy_params;
    proxy_http_version 1.1;
    proxy_buffering off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_pass http://127.0.0.1:5000/socket.io;
}

}

This is the startup statement I use for Gunicorn:

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

EDIT

I have changed my nginx configuration on the /socket.io from proxy_pass http://127.0.0.1:5000/socket.io; to proxy_pass http://127.0.0.1:8000/socket.io; and it now doesn't have that error but when I look at the network tab in the dev tools there is no network activity whatsoever. Not sure if this is another issue??

If you need any more information please let me know and I will supply it!

Any help is appreciated.

Thanks
Conner

but when I look at the network tab in the dev tools there is no network activity whatsoever.

So you are saying that you get a connection, everything works, but there are no requests showing up in the network tab? That's totally normal. Your client is connected via WebSocket, so it is not going to issue any requests to the server once the WebSocket connection is established.

The network tab should have one line that shows the WebSocket request. All the activity is happening on that request, which is long lived.

That's my problem there isn't that one line. The network tab has absolutely nothing. I am not exactly sure what I am doing wrong.

Also I would like to say that you are an awesome person. The fact that you released flask-socketio AND you actually keep up with the issues on Github is incredible. I appreciate your help so much! Thanks a bunch Miguel!

You are very welcome. :)

Did you open the network tab before you connect to your server? The network tab does not have a memory, it will only show you requests that are issued while it is open.

Ill try that right now.

You were right! I do see the connections getting a 200! This is great! I have been stuck for DAYS! Now I have to figure out how to get my streaming Raspberry Pi data to a highchart.js graph I have on my dashboard!

Thank you so much for the help Miguel you have greatly helped me! The world needs more developers like yourself who release a product and then help the users of it for a long time!

Once again thank you so much!

Conner

Was this page helpful?
0 / 5 - 0 ratings