miguelgrinberg / python-socketio

Python Socket.IO server and client
MIT License
3.94k stars 583 forks source link

Emitting event from external process in python-socketio 5.11 doesn't work. #1341

Open ahsanakram786 opened 3 months ago

ahsanakram786 commented 3 months ago

I was using python-socketio==5.8 server configured with django asgi server backend. Every thing was working before upgrading package to python-socketio==5.11.0. And then the event emitting from external Django process stops working,

i m using Redis for message broker with socketio

Here is my code:

from socketio import AsyncRedisManager
from asgiref.sync import async_to_sync

redis_manager = AsyncRedisManager(settings.redis_url)

async_to_sync(redis_manager.emit)('my_custom_callback',
                                payload,
                                namespace='/my_namespace', 
                               room=my_room_name)

And here is my python package versions:

python-engineio               4.8.0
python-socketio               5.11.0
redis                        5.0.1

And here is my redis-server version in linux

Redis server v=7.2.5
miguelgrinberg commented 2 months ago

I'm testing this here and everything seems to work. This is what I've tested:

I modified app.py from the examples/server/asgi directory to use the Redis client manager. Diff:

diff --git a/examples/server/asgi/app.py b/examples/server/asgi/app.py
index 36af85f..0e27387 100644
--- a/examples/server/asgi/app.py
+++ b/examples/server/asgi/app.py
@@ -11,8 +11,10 @@ admin_login = {
 import uvicorn
 import socketio

+mgr = socketio.AsyncRedisManager()
 sio = socketio.AsyncServer(
     async_mode='asgi',
+    client_manager=mgr,
     cors_allowed_origins=None if not instrument else [
         'http://localhost:5000',
         'https://admin.socket.io',  # edit the allowed origins if necessary

I started a Redis service on localhost:6379 and then started app.py. Once running, I connected to localhost:5000 with a web browser and verified that the server was running.

Then I started an async Python console with python -m asyncio and sent a message to the browser with:

>>> import socketio
>>> mgr = socketio.AsyncRedisManager(write_only=True)
>>> await mgr.emit('my_response', {'data': 'Sent from external process'})

This is all working, so now it would be useful to know what's different in your application.