Replies: 1 comment 5 replies
-
Disconnections can happen for a lot of different reasons that are external to the actual server and client, so I'm going to assume that this is something that cannot be helped and that your goal is to figure out how to resume streaming once the clients are reconnected. Looking at your log, the Then when the client reconnects, it should request a new streaming thread to be started by the server. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have python socketio-client, which picks the live data from another websocket and publish them (after processing) onto flask-socketio ip:port using Emit. Socketio server listens to this particular event and on each such data it updates its internal dictionary with the latest data (lets call it "Curve")
Then there is a Web Flask UI, which on loading emits a message to Socketio server, which triggers a background task to process latest "Curve" data and pushes the updates to socketio on "bedata" topic. Web UI updates its table on each update received in socketio ip:port on "bedata".
The setup works like a charm for ~15 mins, after that both Web UI client and Python socketio-Client receives a "Disconnect" event from Flask and after the though both of them are connected back, the streaming stops and doesn't tick anymore!
More details of code is here: https://stackoverflow.com/questions/77084494/flask-socketio-is-disconnecting-web-python-clients-after-15-mins-of-data-str
Using:
flask:2.3.2
flask-socketio:5.3.5
python-socketio::5.8.0
eventlet:0.33.3
WsgiServer of Eventlet on IIS Express
WorkFlow:
Python based SocketIO client(DataStreamer.py) => app.py + MydataClient.py => JS of html (client.html) => GUI (client.html)
init.py:
from flask import Flask
from flask_socketio import SocketIO
from threading import Lock
import eventlet
eventlet.monkey_patch()
app = Flask(name, template_folder='./templates')
app.config['SECRET_KEY'] = 'privatekey!'
thread = None
thread_lock = Lock
socketio = SocketIO(app, cors_allowed_origins='*',ping_timeout=15,logging=True)
app.py:
from wwwroot.StreamingApp import app, thread, thread_lock, socketio
import MydataClient
import eventlet
from eventlet import wsgi
wsgi.server(eventlet.listen(("0.0.0.0", 90)), app)
@app.route("/live")
def main():
return render_template('client.html')
@socketio.on('connect', namespace='/web')
def connect():
print('Client Connected')
@socketio.on('mydata',namespace='/web')
def MsgHandler(data):
MydataClient.Curve = data
MydataClient.py:
from wwwroot.StreamingApp import app, thread, thread_lock, socketio
Curve=None
def CalculateBE():
while True:
response = {'status': True}
for args in live_be:
if args == None:
continue
be_price = #a method which does calculations on "Curve" data defined above
response['BE' + str(args[0])] = be_price
socketio.emit('bedata', response)
socketio.sleep(5)
@socketio.on('be')
def UpdateBE():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(CalculateBE)
client.html (Following is the code in JS of html):
<script> $(document).ready( function () { var flask_socket = io.connect('http://localhost:90'); flask_socket.on('connect',()=>{ console.log('connected!'); }); flask_socket.on('disconnect',()=>{ console.log('disconnected!'); }); flask_socket.emit('be'); flask_socket.on("bedata", function (response) { #Update UI }); }); </script>DataStreamer.py:
import socketio
sio = socketio.Client(reconnection_attempts=5,reconnection_delay=2,reconnection_delay_max=10)
sio.on('connect', on_connect)
sio.on('disconnect', on_disconnect)
sio.on('reconnect', on_reconnect)
sio.connect('http://localhost:90',namespaces=['/web'])
#some calculations from websocket data and then emit
sio.emit("mydata",curve_data,namespace="/web")
Beta Was this translation helpful? Give feedback.
All reactions