Realtiming with Flask-socketIO

Standard

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s