socketio plus json

We’ll use Socket.IO to take JSON-RPC requests. It should respond to “ping” with “pong”.

Install Flask, Flask-SocketIO and eventlet to take requests and jsonrpcserver to process them:

$ pip install flask flask-socketio eventlet jsonrpcserver

Create a server.py:

from flask import Flask
from flask_socketio import SocketIO, send
from jsonrpcserver import method, dispatch

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

@method
def ping():
    return "pong"

@socketio.on("message")
def handle_message(request):
    response = dispatch(request)
    if response.wanted:
        send(response, json=True)

if __name__ == "__main__":
    socketio.run(app, port=5000)

Start the server:

$ python server.py
(27985) wsgi starting up on http://127.0.0.1:5000

Client

TODO.