When multiple clients connect to my socketio flask application, how can I achieve per-client settings?
What I have:
@socketio.on('replay-start')
def replay(message):
while True:
if not paused:
emit('replay', dict(data=f'private replay'))
socketio.sleep(1)
Now if a client sends a pause event, I want only the clients loop to pause.
If I implement it like this:
@socketio.on('replay-pause')
def replay_pause(message):
global paused
paused = True
Of course this pauses all loops, and not just the one of the current client. Is there some way to achieve this? Maybe there is some "context object" where I can see the id of the client sending the message?
The most convenient way to store per-client settings is to use the Flask user session, which does work (with certain limitations) on Socket.IO handlers:
from flask import Flask, render_template, request, session
@socketio.on('replay-start')
def replay(message):
while True:
if not session.get('paused'):
emit('replay', dict(data=f'private replay'))
socketio.sleep(1)
@socketio.on('replay-pause')
def replay_pause(message):
session['paused'] = True