I am using Flask-socket.io. It works, but it keeps connecting and disconnecting. so first time it is not getting notification cause connection keeps connecting and disconnecting.
This is frontend code.
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js" integrity="sha384-2huaZvOR9iDzHqslqwpR87isEmrfxqyWOF7hr7BY6KG0+hVKLoEXMPUJw3ynWuhO" crossorigin="anonymous"></script>
<script>
// initialize socket for notification
const socket = io('/notification', {
reconnectionDelay: 2000,
reconnectionDelayMax: 4000,
ping_timeout: 6000
});
...
And this is backend code
app = create_app()
babel = Babel(app, locale_selector=get_locale)
mail = Mail(app)
socketio = SocketIO(app)
socketio.init_app(app, cors_allowed_origins="*")
@socketio.on('connect', namespace='/notification')
def handle_connect():
if current_user.is_authenticated:
join_room(current_user.username)
print('connected to', current_user.username)
return True
@socketio.on('disconnect', namespace='/notification')
def handle_disconnect():
if not current_user.is_authenticated:
leave_room(current_user.username)
print("disconnected")
return True
Can anyone help me this? I am really appreciate. Thanks.