cereme
cereme

Reputation: 280

flask-socketio does not start/works wierd on aws ec2

This is my flask code,

from flask import Flask, render_template
from flask_socketio import SocketIO, emit

app = Flask(__name__)
socketio = SocketIO(app)

@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('connect')
def socketioconnect():
    emit('customresponse', {'msg':'connect emit'})

@socketio.on('cunstommsg')
def custommsgHandler(msg):
    emit("customresponse", {'msg':msg['msg']+'with handler emit'})

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5000)

and when i run this code with "sudo python3 app.py", i can see only

INFO:engineio:Server initialized for gevent.

also when i acess with web browser, nothing changes in both of console and browser

and inbound setting of my ec2 instance is open with port 80, 5000

and this is my pip freeze

click==6.7
Flask==1.0.2
Flask-SocketIO==3.0.1
gevent==1.3.5
greenlet==0.4.14
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
pkg-resources==0.0.0
python-engineio==2.2.0
python-socketio==2.0.0
six==1.11.0
Werkzeug==0.14.1

i tried this and it works:

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80)
    socketio.run(app, host='0.0.0.0', port=5000)

but all of tutorials and SO answers say don't do like this

this is client test code:

  var socket = io();
  socket.on("customresponse", function(msg){
      console.log("cumstom response arrived"+ msg.msg);
      alert("cumstom response arrived" + msg.msg);
  });

  $(function () {
      $("#sendSIO").click(function() {
          console.log('sendSIO clicked');
          socket.emit("custommsg", {msg:'sendSIO clicked'});
      });
  });

when i connect, i can get "custom response arrived" in javascript console but when i clicked #sendSIO, although i can see "custommsg arrived" in flask console, client's "customresponse" handler does not work.

is this problem related with run() of flask-socketio?

sorry for kind of untidy question, but im noob to socketio and there's no useful documents for this kind of issues. Thanks!

Upvotes: 0

Views: 1408

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67479

It seems you want to run your application on port 80, so you should do this:

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=80)

Upvotes: 2

Related Questions