Jasdev Sidhu
Jasdev Sidhu

Reputation: 11

setting up gevent-socket using flask,nginx and gunicorn

The following is my python code

from gevent import monkey;monkey.patch_all()
from flask import Flask,render_template, url_for, request, redirect, flash,jsonify,session,Markup
from socketio import socketio_manage
from socketio.namespace import BaseNamespace
from socketio.server import SocketIOServer

app=Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'

class ChatNamespace(BaseNamespace):
  def recv_connect(self):
    print "successfully connected"
    self.emit('show_result','successfully connect')
  def on_receive_message(self,msg):
    print "message is "+msg["data"]
    self.emit('show_result2',msg["data"])
@app.route('/')
def index():
   #print "in the function"
   return render_template('index.html')

@app.route("/socket.io/<path:path>")
 def run_socketio(path):
  socketio_manage(request.environ, {'/test': ChatNamespace})
  return 'ok'

if __name__=='__main__':
  #app.run(debug=True, port=80, host='0.0.0.0')
   app.debug=True
   #app.run()
   SocketIOServer(('0.0.0.0', 5000), app,resource="socket.io").serve_forever()
print "successfull listen to socket"

and the following is nginx configuration server {

location / {
  proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  try_files $uri @proxy;
}
location @proxy {
    proxy_pass http://127.0.0.1:8000;
}
location /templates {
    alias  /home/www/flask_project/templates/;
}
 location /script {
    alias  /home/www/flask_project/script/;
  }
  location /static {
    alias  /home/www/flask_project/static/;
  }

 }

Each time I run the app ,I use the following command
gunicorn main2:app -b localhost:5000

I know that I am missing a lot of information to run this gevent-socketio app on a live server. Can anyone help me out, I am totally new to this web-socket technology

Upvotes: 0

Views: 687

Answers (1)

CESCO
CESCO

Reputation: 7768

Have you tried FlaskSocketio??

If you are trying to do it without the extension, you will need the socketio gunicorn worker to run your app.

gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app

Upvotes: 0

Related Questions