0

I have this script:

from flask import Flask
from flask_socketio import SocketIO, send

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

def run_server():
    socketio.run(app)


@socketio.on('message')
def handleMessage(msg):
    print('Message: ' + msg)
    send(msg)


if __name__ == '__main__':
    socketio.start_background_task(run_server)

Every time i'm running that script the program begin and finish immediately.

I want to have both a Web server and a SocketIO server at the same app.

Maybe by having a port that listens to simple HTTP requests and a port that listens for SocketIO requests.

Inbar Cheffer
  • 2,109
  • 1
  • 7
  • 8
  • 1
    What you don't seem to realize is that Socket.IO runs on the same web server as your application, you don't need to servers, one is enough. The Socket.IO portion is namespaced to the `/socket.io` URL, so any other URLs that clients send will go to your Flask server. – Miguel Grinberg Mar 25 '18 at 15:14
  • First of all that is genious, i just thought it has a different application protocols so it should be on different ports. but because Im using flask_manager i still don't know how to combine socketio? and can you tell me does a C++ socketio client can communicate with flask_socketio? and thanks for everything you're really helpfull!! – Inbar Cheffer Mar 26 '18 at 15:22
  • If you run your application with `socketio.run(app)`, then all the work is done for you and both your app and your Socket.IO server will be hosted on a single web server. Regarding the C++ client, absolutely, I know of people who are using it successfully. – Miguel Grinberg Mar 26 '18 at 15:59
  • @Miguel I'll try modify the application as you say, also going to read your book on web developing with flask though I haven't saw a chapter dedicated for Socket.IO. I'll try looking for a C++ client library and wrap it up with my class. – Inbar Cheffer Mar 26 '18 at 23:01
  • There is an official Socket.IO client for C++ from the Socket.IO organization. Look it up on GitHub. – Miguel Grinberg Mar 27 '18 at 02:50
  • Yes, I saw that, it looks great, though I also saw some people says it's not working for them. @Miguel I got some troubles in my app that I can't manage to solve, I wonder if I can get your info and maybe you'll help me, or at least if I can send you my app and you'll see what I'm talking about and give me some notes on how to fix those problems? – Inbar Cheffer Mar 28 '18 at 19:38

1 Answers1

2

I'm starting my server like this :

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


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

And you can mix your routes in your app so you can have something like this :

@app.route('/picture')
def picture():
    """
    Generate a picture, save it, and send it to client
    """
    path_picture = ironcar.picture()
    print('path_picture : ', path_picture)

    if path_picture:
        r = send_file(path_picture,
                        as_attachment=True)
        r.headers["Pragma"] = "no-cache"
        r.headers["Expires"] = "0"
        r.headers['Cache-Control'] = 'public, max-age=0'
        return r


# ------- SOCKETS ----------
@socketio.on('mode_update')
def mode_update(mode):
    """
    Change the driving mode of the car
    """
    print('SERVER : mode: ' + mode)
    ironcar.switch_mode(mode)
Hugo
  • 1,106
  • 15
  • 25
  • 1
    If you want to see how I use it in my project you can take a look at this [repo](https://github.com/Hugoo/ironcar). – Hugo Mar 24 '18 at 10:01
  • I'll read it, but as I see you're activating the socketio and it also activate the app, what are their ports? is it the same port? now server listens for both HTTP and SocketIO requests? – Inbar Cheffer Mar 24 '18 at 17:15
  • Yes everything runs on the same server – Hugo Mar 24 '18 at 17:24
  • Is that ok to have both web-app and socketio running on port 443 if I want to use https for my website? – Pavlo Sharhan Nov 24 '22 at 16:30