I'm trying to connect my backend (python, flask) and frontend (react) with sockets, but the every time the sockets are around 20 seconds late.
backend:
from flask_socketio import SocketIO, emit
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins="*")
CORS(app)
socketio.emit('message', 'test')
if __name__ == '__main__':
socketio.run(app, debug=True, host='0.0.0.0')
frontend:
const App = () => {
const [message, setMessage] = useState("")
useEffect(() => {
const socket = io('http://localhost:5000')
socket.on("message", data => {
setMessage(data)
})
}, [])
return (
<div>
{message}
</div>
)
}
So the word test appears 20 seconds after loading the page. Why? Is it possible to do it faster?