I’m using flask-socketio to send data between a python web server and a client web page.
Server Code :
from flask import Flask, render_template, send_from_directory
from flask_socketio import SocketIO,emit
app=Flask(__name__, static_url_path='', static_folder="static/")
socketio=SocketIO(app)
@socketio.on('connect')
def test_connect():
print("socket connected")
if __name__=='__main__':
socketio.run(app)
Javascript client code :
const socket=io('http://localhost:5000');
socket.on('connect',()=>{
console.log('connected!');
}
Now,when I run it locally on my computer,it works fine.
But when I deployed it on Glitch and ran it, It gave me few warnings as :
Serving Flask app (lazy loading)
Environment: production
WARNING: Do not use the development server in a production environment. Use a production WSGI server instead.
Debug mode: on
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Restarting with stat WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.
Debugger is active!
Debugger PIN: 327-937-508
And my code wasn’t working. I was able to open index.html but the console didn’t log “socket connected”. Also, none of my other socket emits were working.
After going through a few stack overflow questions, I changed my server code to :
from flask import Flask, render_template, send_from_directory
from flask_socketio import SocketIO,emit
from gevent.pywsgi import WSGIServer
app=Flask(__name__, static_url_path='', static_folder="static/")
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
socketio=SocketIO(app)
@socketio.on('connect')
def test_connect():
print("socket connected")
if __name__=='__main__':
socketio.run(app)
This fixed the warnings but everything still doesn’t work.
Any way to fix this?