I'm using python-socketio
for the first time, I have a simple AsyncServer
that looks like this.
import socketio
from aiohttp import web
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
@sio.event
async def connect(sid, environ):
print("Connected")
@sio.event
def set_name(sid, name):
print("Set name: " + name)
if __name__ == "__main__":
web.run_app(app)
And I have a client that looks like this.
import socketio
sio = socketio.AsyncClient()
async def start():
await sio.connect("http://localhost:8080", transports=['websocket'])
await sio.wait()
async def set_username(name):
await sio.emit("set_name", name)
On the client, I use asyncio.run
on the start
function, and it connects successfully and instantly. However, later on when I do the same with the set_username
function, it's EXTREMELY slow and takes a minute for the event to be recognized on the server-side.
I've tried using event loops in asyncio
with no luck. I searched for similar problems on the Internet, found similar ones but none of their solutions worked for me.
Thanks for mentioning the versions of the libraries you have, this helped me to double check.
You mentioned you tried to run this code either with
eventloop
orasyncio.run
and both had no success. The way you run your client is crucial here as the setup on the server is okay. You said at first you runconnect
and then you runset_username
.Not sure how exactly the
set_username
got triggered in your cases, but in the code you shared it simply isn't.Please see the two scripts attached below as an example of how it could lok like and let me know if they work for you. What code does should be clear from the doc strings I hope.
At first run the
python3 server.py
and thenpython3 client.py
. After that you should see the logs both on the server and on the client:set_name
event every 1 second, so server will be logging it.server-message
to the client, so client will be logging it.I hope this works