I'm interested in creating bidirectional exchange between Flask server and client. Seems that websockets are very convinient for achieving my goal. But i stucked on implementation. I've created simple Flask app with Flask-SocketIO like described in docs:
from flask_socketio import SocketIO
from flask import Flask
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@socketio.on('message')
def handle_message(message):
print('received message: ' + message)
if __name__ == '__main__':
socketio.run(app)
Client uses websocket-client package and looks as follows:
from websocket import create_connection
ws = create_connection("ws://localhost:5000")
ws.send("hello world")
In this configuration it doesn't work.
Also i tried to use Flask-Sockets on the server side:
from flask import Flask
from flask_sockets import Sockets
app = Flask(__name__)
sockets = Sockets(app)
@sockets.route('/')
def echo_socket(ws):
while not ws.closed:
message = ws.receive()
print(message)
if __name__ == '__main__':
app.run()
It also doesn't work. I found several posts in some places (for example there and there) with "working" examples but actually they all don't work.
So is it possible to do what i need? If yes, how to do that? If no, what is alternatives?