From this point in code forward, we start to see the major differences between the RESTful API server and this Web Socket server:
# Flask-SocketIO Callback Handlers
@socketio.on('connect') # (4)
def handle_connect():
logger.info("Client {} connected.".format(request.sid)) # (5)
# Send initializating data to newly connected client.
emit("led", state) # (6)
We see, on line (4), how to register a message or event handler using the Python decorator notation. The parameter to each @socketio.on(<event_name>) is the name of an event our server will listen for. The connect and disconnect events (in the following) are two reserved events. These handlers are called whenever a client connects to or disconnects from the server.
You will notice, on line (5), we are logging whenever a client connects, along with...