Realtiming with Flask-socketIO


Small, realtimed applications are just beautiful. One can think of an idea, and implement it in no time, and still have a very lively app. I was searching for realtime possibilities which can couple up with Flask, and found a pretty decent solution, a Flask extension, Flask-socketIO.

In a nutshell, socketio is a nodejs realtime app framework, which allows realtimed bidirectional communication.

Since we are using Flask (python), and socketio is written in JavaScript, Flask-socketIO is in turn dependent on gevent-socketio.

On to the code, we need the following pythonic requirements:

  1. gevent-socketio
  2. flask
  3. Flask-socketIO
  4. gevent-websocket

Flask-socketIO makes it very simple to implement event based communication.

Here is a sample server code:

And this is the client JavaScript (Socket.IO required):

Messages in server logs:

Screenshot from 2014-12-20 23:01:17

Messages in client JavaScript console:

Screenshot from 2014-12-20 23:01:14

This example is extremely simple, and I hope is self-explanatory. The Flask-SocketIO provides a lot more than this, here are the links to the Flask-SocketIO documentation and this example’s source.

Another project of mine, “CodeCast“, has a ┬ámore practical implementation of Flask-socketIO, which might provide a better understanding of the scenario.