This post uses Jsonrpcserver 5 which requires Python 3.8+.

socketio 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, Result, Success, dispatch

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


@method
def ping() -> Result:
    return Success("pong")


@socketio.on("message")
def handle_message(request):
    if response := dispatch(request):
        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.