Why SocketIO and SSLify not working together?

Viewed 112

I built a very simple flask app that uses socketIo in order to make a simple chatapp, where you can write messages. In my js file i built a function that every few seconds sends the current location of the user using geolocation.

Because im using the geolocation, i need my to use https instead of http that the flask created.

I found out about SSLify and tried it, but because im using also the socketIo it ran on http and not on https. In apps without socketIo it works, what is the problem?

this is my flask app:

from flask import Flask, render_template
from flask_socketio import SocketIO
from flask_sslify import SSLify

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

socketio = SocketIO(app)
sslify = SSLify(app)


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


def messageRecived():
    print('message was received!!!')


@socketio.on('my event')
def handle_my_custom_event(json):
    print('recived my event: ' + str(json))
    socketio.emit('my response', json, callback=messageRecived)


if __name__ == '__main__':
    socketio.run(app, debug=True, host='0.0.0.0')
1 Answers

The reason it does not work is that Flask-SSLify is applied to your Flask application (the app instance), and not to Socket.IO (the socketio instance).

To my knowledge there is no equivalent to Flask-SSLify for the Socket.IO server, so you will need to either build it yourself as a WSGI middleware, or else implement the HTTP to HTTPS redirection in a reverse proxy that sits above your Python server, such as nginx or Apache.

Related