'Flask-SocketIO using emit outside decorator doesn't work
I have a listener function that would be called every time a value changes, and I want socketio to emit this new value.
This is what I've come up at last but it still hasn't worked out. Why doesn't socketio.emit work outside of context when it should? If it doesn't, how to emit within a listener function?
socketio in decorator vs view:
@socketio.on('my event')
def connected(json):
join_room(room = current_user.id)
leave_room(room=rooms()[0]) # leave previous room
print(rooms(), flush=True)
socketio.emit('echo', 'hi', room=current_user.id) # this works fine
@app.route('/')
def home():
socketio.emit('echo', 'hi2', room=current_user.id) # this does not
return render_template("home.html", session = session)
my listener function inside a view:
def someotherview():
itemList = [a,b,c]
@copy_current_request_context
def listener(event):
item = str(event)
if item in itemList:
print(item, event, flush=True) # this prints
socketio.emit('echo', 'test', room=current_user.id) # this also doesnt do anything
Console log from website: only 'hi', no 'hi2' which indicates the prior statement.

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
