'Flask server not starting for SocketIO
I am trying to make a chat app using SocketIO and following this video: https://www.youtube.com/watch?v=RdSrkkrj3l4 and the corresponding GitHub code: https://github.com/PrettyPrinted/flask-socketio-chat
This file is my main.py
file
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template("index.html")
@socketio.on('message')
def handleMessage(msg):
print("Message:" + msg)
send(msg, broadcast=True)
if __name__ == '__main__':
socketio.run(app)
and this is my index.html page
<html>
<head>
<title>Chat Room</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function() {
var socket = io.connect('http://127.0.0.1:5000');
socket.on('connect', function() {
socket.send('User has connected!');
});
socket.on('message', function(msg) {
$("#messages").append('<li>'+msg+'</li>');
console.log('Received message');
});
$('#sendbutton').on('click', function() {
socket.send($('#myMessage').val());
$('#myMessage').val('');
});
});
</script>
<ul id="messages"></ul>
<input type="text" id="myMessage">
<button id="sendbutton">Send</button>
</body>
</html>
However when I run the flask code using python main.py. It never loads anything and just shows this:
C:\Users\Harshit Parikh\Desktop\app>python main.py
Please help me out.
Solution 1:[1]
I used app.run()
instead of socketio.run(app)
, like Caio suggested in the comment below the question. That worked for me.
After it started, I switched back to socketio.run
and my application still works. Perhaps socketio needed some kind of kickstart?
Solution 2:[2]
It worked when using a virtual environment and installing all the libraries again.
Solution 3:[3]
I ran into similar problem. I came across this question. I unistalled greenlet and the code worked fine. I don't know why that happend. But code worked fine now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Joel Oduro-Afriyie |
Solution 2 | Harshit Parikh |
Solution 3 | Ronstha |