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 methods
from jsonrpcserver.response import NotificationResponse

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

@methods.add
def ping():
    return 'pong'

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