pyropy / fastapi-socketio

Easily integrate socket.io with your FastAPI app 🚀
Apache License 2.0
328 stars 31 forks source link

multiple CORS values in socketio requests error when using CORSMiddleware #28

Open chinchaun opened 2 years ago

chinchaun commented 2 years ago

Hello humans, I faced an issue when I was trying to use fastapi-socketio and the CORSMiddleware

app = FastAPI()
sio = SocketManager(app=app)

# CORS
# Set all CORS enabled origins
if config.BACKEND_CORS_ORIGINS:
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

and this error was showing up

Screen Shot 2021-12-30 at 23 13 11

The workaround I found was to set to an empty list the cors_allowed_origins parameter on the SocketManager to prevent the duplication of the values Solution

app = FastAPI()
sio = SocketManager(app=app, cors_allowed_origins=[])

# CORS
# Set all CORS enabled origins
if config.BACKEND_CORS_ORIGINS:
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

The solution was taken from here

if I have some time, I will try to fix it, Cheers

niteshgaba commented 2 years ago

Hi @chinchaun ,

I am also facing the same issue but not able to connect after using the same solution. Any insights ?

chinchaun commented 2 years ago

Hey @niteshgaba, mmm I don't know how to help you, I remember I have to dig into the source code and the issue mentioned above to find the workaround and I just worked.