Mounting Socket.io on a sub-application path other than "/" results in CORS errors #8781
-
First Check
Commit to Help
Example Code#backend/app.py
import socketio
from fastapi import FastAPI
app = FastAPI()
sio = socketio.AsyncServer(async_mode="asgi", cors_allowed_origins="*")
sio_app = socketio.ASGIApp(socketio_server=sio, socketio_path="socket.io")
# Does not work if this is anything other an a forward slash
app.mount("/", sio_app)
@sio.event
def connect(sid, environ, auth):
print('connect ', sid)
@sio.event
def disconnect(sid):
print('disconnect ', sid)
#####################################################
# frontend/App.js
import { io } from "socket.io-client";
function App() {
// Cannot connect to subapplication path such as: ws://127.0.0.1:8000/ws
const socket = io("ws://127.0.0.1:8000", {
path: "/socket.io/" ,
autoConnect: false
});
const handleConnect = () => {
console.log("Button Clicked to Connect")
socket.connect();
}
const handleDisconnect = () => {
console.log("Button Clicked to Disconnect")
socket.disconnect();
}
return (
<div className="App">
<button onClick={() => handleConnect()}>Connect</button>
<button onClick={() => handleDisconnect()}>Disconnect</button>
</div>
);
}
export default App; DescriptionBackend is on http://localhost:8000 In the event that the mount path is changed to anything other than For example, if it is mounted to
If this same
Then there is no console output whatsoever. In the event this isn't an issue, could a working example be supplied as to how a javascript frontend may still communicate with a socket-io subapplication with mounted by FastAPI (i.e. mounted to Operating SystemLinux Operating System DetailsNo response FastAPI Version0.68.0 Python Version3.8.10 Additional ContextFile Structure attached for replication. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi 👋, I seem to have reached a working version: Server sideimport 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 sideimport { 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("Button Clicked to Connect");
socket.connect();
};
const handleDisconnect = () => {
console.log("Button Clicked to Disconnect");
socket.disconnect();
};
socket.on("message", (e) => console.log("Received", e));
return (
<div className="App">
<button onClick={() => handleConnect()}>Connect</button>
<button onClick={() => handleDisconnect()}>Disconnect</button>
</div>
);
}
export default App; I added a message sent on connection to your example. There are a few small differences, but most issues I ended up encountering were related to Socket.io struggling to understand that |
Beta Was this translation helpful? Give feedback.
-
This answer doesn't work for me. Have things changed? |
Beta Was this translation helpful? Give feedback.
Hi 👋,
I seem to have reached a working version:
Server side
Client side