I’m having trouble using socketio between python and reactjs. The issue is when I try the code with multiple tabs in same browser or in different browser. Not every client receive the broadcast message. I tried adding forceNew=True and a query parameter with a random number but none of the solution worked.
In python i’m using the below code
self.sio = socketio.AsyncServer( async_mode='asgi', cors_allowed_origins= settings.CORS_ALLOWED_ORIGINS )
@self.sio.event
async def connect(sid, environ):
# does nothing now.
pass
@self.sio.event
async def disconnect(sid):
# does nothing now.
pass
@self.sio.event
async def STOR(sid, data):
# Broadcast to all
await self.sio.emit("STOR", json.dumps({
'success': True,
'data':{}
}))
# acknowledgment to the sender.
return json.dumps({
'success': True,
'data':{}
} ;
@self.sio.on('*')
async def any_event(event, sid, data):
pass
In react side
export const socket = io(SOCKET_URL, {
secure: true,
forceNew: true,
transports: ['websocket', 'polling'],
upgrade: true
});
useEffect(() => {
const handleBroadcast = (msg) => {
//
}
socket.on('STOR', handleBroadcast);
return () => {
socket.off('STOR', handleBroadcast);
};
}, [socket]);