79579971

Date: 2025-04-17 19:45:33
Score: 3.5
Natty:
Report link

can you share the complete script, I am not able to connect to websocket server.

import socketio
from socketio.async_redis_manager import AsyncRedisManager
from starlette.applications import Starlette
from starlette.routing import Route, Mount
from starlette.responses import JSONResponse
import uvicorn

# 1. Configure Redis-backed manager for pub/sub
mgr = socketio.AsyncRedisManager("redis://localhost:6379/0")

# 2. Create Socket.IO server with Redis client_manager
sio = socketio.AsyncServer(
    async_mode="asgi",
    client_manager=mgr,          # ← crucial for syncing across instances :contentReference[oaicite:1]{index=1}
    cors_allowed_origins="*",
)

# 3. Mount Socket.IO as an ASGI app under /ws
ws_app = socketio.ASGIApp(sio, socketio_path="test")

# 4. Define HTTP route just for sanity check
async def homepage(request):
    return JSONResponse({"message": "Socket.IO + Redis server is up"})

# 5. Wire up Starlette routes
app = Starlette(debug=True, routes=[
    Route("/", homepage),
    Mount("/ws", ws_app),
])

# 6. Socket.IO event handlers
@sio.event
async def connect(sid, environ):
    print(f"Client connected: {sid}")
    await sio.send(sid, "Welcome!")

@sio.event
async def message(sid, data):
    print(f"Received from {sid}: {data}")
    await sio.send(sid, f"Echo: {data}")

@sio.event
async def disconnect(sid):
    print(f"Client disconnected: {sid}")

# 7. Run with Uvicorn
if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)

I trying to connect using ws://localhost:8000/test

Reasons:
  • Blacklisted phrase (1): I am not able to
  • RegEx Blacklisted phrase (2.5): can you share
  • Long answer (-1):
  • Has code block (-0.5):
  • Starts with a question (0.5): can you share the
  • Low reputation (1):
Posted by: Ajaykumar Kanojiya