Skip to content

Mounting Socket.io on a sub-application path other than "/" results in CORS errors #8781

Discussion options

You must be logged in to vote

Hi 👋,

I seem to have reached a working version:

Server side

import socketio
from fastapi import FastAPI


app = FastAPI()
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
sio_app = socketio.ASGIApp(socketio_server=sio)

app.mount("/ws", sio_app)


@sio.event
async def connect(sid, environ, auth):
    print("connect ", sid)
    await sio.emit("message", f"connected {sid}", room=sid)


@sio.event
def disconnect(sid):
    print("disconnect", sid)

Client side

import { io } from "socket.io-client";

function App() {
  const socket = io("ws://127.0.0.1:8000/", {
    path: "/ws/socket.io",
    autoConnect: false,
  });

  const handleConnect = () => {
    console.log("Butt…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Kludex
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
3 participants
Converted from issue

This discussion was converted from issue #3670 on February 28, 2023 16:36.