Flask Socket IO return status 400 When deploy to Server

Viewed 68

I using flask socket io to implementing the notification to my multiple clients using my application.

The flask socket is working well on my local development but the problem happen I deployed it to use on the server. It most of the time returned status 400 frequently and down the server sometime. I tried digging into source to root cause but I could not.

Below is a snippet that I had a problem:

Here is an init file for initializing application

from __future__ import absolute_import

import os
import redis
import flask_sqlalchemy as sa

from flask import Flask
from flask_socketio import SocketIO
from flask_cors import CORS

from .core.constant import MKT_BLUEPRINT, admin

# Initialize the core application and configure the app
app = Flask(__name__, instance_relative_config=True)
CORS(app)

# Initialize the core application and configure the app
app.config.from_object('mkt.config')

# redis address: "redis://localhost:6379/0"
redis_add = "redis://localhost:6379/0"

async_mode = None
notify_socketio = SocketIO(app, cors_allowed_origins="*", 
                                message_queue=redis_add, 
                                async_mode=async_mode
                            )
db = sa.SQLAlchemy(app)
app.register_blueprint(admin, url_prefix='/%s'%MKT_BLUEPRINT)

And then on client side, I use this js script

$(document).ready(function() {

        var domain = "{{notify_domain|safe}}"

        var socket = io(domain);

        socket.on('connect', function() {
            var user = "{{user|safe}}"
            console.log("im in connect", user)
            socket.emit('join_room', {room: user});
        });

    });

Then on server I config the nginx as below:

server {
    listen 443 ssl;
    server_name xxx;
    include /config/nginx/ssl.conf;

    location / {
        proxy_read_timeout 300s;
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        proxy_pass http://xxx:8000;
    }
    location /socket.io {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Access-Control-Allow-Origin *;

        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://xxx:8000/socket.io;
    }
}

And here are dependencies that I'm using in my application:

#for notification to client
Flask-SocketIO==5.1.1

#for notification to client
Flask-SocketIO==5.1.1
python-engineio==4.2.1
python-socketio==5.4.0

Without any misconfiguration, I logged in console and below enter image description here

Till now I still could not know what caused this error 400 (bad request) and solved it. Please kindly help. Thank so much.

0 Answers
Related