-
-
Notifications
You must be signed in to change notification settings - Fork 595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not reconnecting when used with FastAPI server #622
Comments
Can you provide a small example that shows the problem? |
Sure! I was able to reproduce the issue with this client and server setup. Using this as the client. # socketio client + FastAPI server
import socketio
import uvicorn
from fastapi import FastAPI
app = FastAPI()
sio = socketio.AsyncClient(logger=True, engineio_logger=True)
@app.on_event('startup')
async def startup():
await sio.connect('http://localhost:8080')
@app.on_event('shutdown')
async def shutdown():
await sio.disconnect()
@app.get('/')
async def root():
return {'message': 'Hello world'}
@sio.event
async def connect():
print('connection established')
await sio.send('Hello server')
@sio.on('redemption')
async def on_message(data):
print('Channel points redeemed!', data)
@sio.event
async def disconnect():
print('disconnected from server')
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8000) And this as the server. # socketio server (copied from the example)
from aiohttp import web
import socketio
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
async def index(request):
"""Serve the client-side application."""
with open('index.html') as f:
return web.Response(text=f.read(), content_type='text/html')
@sio.event
def connect(sid, environ):
print("connect ", sid)
@sio.event
async def chat_message(sid, data):
print("message ", data)
await sio.emit('reply', room=sid)
@sio.event
def disconnect(sid):
print('disconnect ', sid)
app.router.add_static('/static', 'static')
app.router.add_get('/', index)
if __name__ == '__main__':
web.run_app(app) Results in:
|
@cetteup Okay, I did find something wrong in the asyncio client reconnects. Your examples apps were useful, thanks for spending the time and creating them. With the fix applied in this repository on master the reconnection works. However, there is another related issue that I'm seeing. When the Socket.IO log in the client shows the line |
Thank for you for pushing a fix. And yes, I can confirm the long delay.
|
Thanks, I'll see if I can figure that one out as well. |
Thank's a lot for you work on this! In the meantime, I just re-installed from the master branch and I can confirm that the reconnect now works as intended. |
I am trying to set up a project that a) runs a local FastAPI server in order to provide a web ui/api for settings etc and b) runs a socket.io client listening for events.
I set up both the FastAPI app and the socket.io async client globally.
Then run the FastAPI app via uvicorn.
And connect to the node.js-based socket io server as one of the startup tasks.
Which works fine. The FastAPI server starts up and does it's thing and so does the socketio client. However, if the connection to the server is lost for some reason, the client never reconnects.
That when it stops doing anything. I read through most of the similar issues in here, but unless I misunderstood, the issue should actually be fixed.
If I instead use a more default async client script, it reconnects without any issues.
Versions I am using:
python 3.8.7 (Windows 64-bit)
socket.io 3.1.0 (on node.js 14)
fastapi 0.63.0
python-engineio 4.0.0
python-socketio 5.0.4
websockets 8.1
aiohttp 3.7.3
The text was updated successfully, but these errors were encountered: