0

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?

1
  • Check compatibility between Flask-SocketIO and javascript client , Flask-SocketIO==4.x needs cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js Commented Aug 8, 2024 at 10:28

1 Answer 1

1

Okay. So I got it working

The problem was with client side being io('http://localhost:5000') , since the client is no longer running locally.

I fixed it by changing client to io() and server to socketio.run(app,port=3000)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.