admin管理员组文章数量:1123666
I'm building a Flask application where I use Redis to receive data from a server, and then send that data to the frontend using Socket.IO. However, the emit() function is not sending the data to the frontend as expected.
Here is my Redis subscriber function:
def redis_subscriber():
pubsub = r.pubsub()
pubsub.subscribe('feed_channel')
print("Subscribed to feed_channel")
for message in pubsub.listen():
if message['type'] == 'message':
try:
data = message['data'].decode('utf-8')
# print(f"Emitting Redis data: {data}")
socketio.emit('new_data', json.dumps({"message": "Connected to Flask server"}))
except Exception as e:
print(f"Error processing message: {e}")
The Redis subscription is working fine, and I can confirm that messages are being received. However, the socketio.emit() call doesn't seem to send the data to the frontend.
Redis is properly set up and publishing messages to the feed_channel. The Socket.IO server is running and connected to the frontend.
What could be causing the emit() function to fail in sending the data via WebSocket?
I'm building a Flask application where I use Redis to receive data from a server, and then send that data to the frontend using Socket.IO. However, the emit() function is not sending the data to the frontend as expected.
Here is my Redis subscriber function:
def redis_subscriber():
pubsub = r.pubsub()
pubsub.subscribe('feed_channel')
print("Subscribed to feed_channel")
for message in pubsub.listen():
if message['type'] == 'message':
try:
data = message['data'].decode('utf-8')
# print(f"Emitting Redis data: {data}")
socketio.emit('new_data', json.dumps({"message": "Connected to Flask server"}))
except Exception as e:
print(f"Error processing message: {e}")
The Redis subscription is working fine, and I can confirm that messages are being received. However, the socketio.emit() call doesn't seem to send the data to the frontend.
Redis is properly set up and publishing messages to the feed_channel. The Socket.IO server is running and connected to the frontend.
What could be causing the emit() function to fail in sending the data via WebSocket?
Share Improve this question asked 23 hours ago sanabil mustafasanabil mustafa 11 bronze badge1 Answer
Reset to default 0pubsub.listen()
is blocking, your redis_subscriber function gets stuck inside the for message in pubsub.listen():
loop.
It waits indefinitely for a new message from Redis
/Valkey
.
You take the whole main thread for the subscriber and the socketio
engine don't get the chance to actually do something with the data because the loop keep going.
If you want to have an async API, move to valkey-glide which will also give you better pub sub fault tolerance.
If you would like to keep using sync API, use socketio.start_background_task()
(more recommended in most cases) or threading.Thread
.
本文标签: flasksocketioemit() not running inside redis functionStack Overflow
版权声明:本文标题:flask - socket.io.emit() not running inside redis function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736583901a1944984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论