Tag Archives: python

Ping Pong with gevent, socket-io and flask

So recently one of my project allow me to have experience on gevent-socketio. While the code is open source, it can be complicated. Thus the example I show here, while not serious hopefully will make things easier to see.

You will need these dependency,

Before we can start to create a view for socket-io you will need to define the namespace to be used

The gevent-socketio library uses Namespace as the view logic, all the logics will be in a namespace, along with namespace, there is also Mixins that is built on top of namespace that provide extra functionality, gevent-socketio provide BroadcastMixin and RoomMixin, which is useful.

line 3: We create our namespace by subclassing Namespace Class

line 4: This is method defined by us. We prefix on_ to an event that we want to process. In this case it is the ping event. The event will be send from socketio client. The parameter attack, that is essentially the payload that we will be receiving, and we don’t need to do any conversion from json to dict, as the library handle it for us.

line 7 and 9: Self.emit will emit an event to the socketio client, again the dict, is the payload that will be sent, no converstion to json necessary.

Now we can finally define a view

This is the view for socketio. app is just your standard flask app.

line 7: is the route necessary for the socketio client to connect

line 9: this is the same across socketio app, the difference here is the namespace ‘/pingpong’ and the namespace PingPong, that you will need to define yourself.

Now to serve this guy

line 6: this will be the line that serve the app. It will serve all the views include the non-socketio one.

I will skip the view to render the main page and the html, I will focus on the javascript alone.

line 2: This is how you define a socket io.connect(‘/namespace’)

line 10: We bind a javascript click an event, to send an event we use socket.emit(event,data). data will be a javascript object, event will be a string, notice the on_ping method on the Namespace we define on top.

line 3: As the socketio server emit a pong event, this will event handler and do something with it in callback. Again don’t worry about conversion, the data is converted into a javascript object.

Hopefully this will make things slightly clearer. Btw the example on github.


Adventure in Overriding Python Str

Recently I have given a project where we are overriding the python str object, I rather not say the reason because it is job related. But the process of it is pretty general, and is pretty interesting.

So here is an example.

A few thing to take note here.

  1. Notice that there is a __new__ method? In python object lifecycle, the first thing to be called is actually __new__ not __init__. __new__ will choose whether whether to replace __init__ or use a new one. In this case, I set the current object cls to str, with value bleeped
  2. Now if object is not setup in __new__ yet, how do object access the bleep method? Introducing, classmethod. Classmethod is a method the bind to a class, not an object. This is useful if you want to do call a function before a class is initiated.
  3. Classmethod binds to the child class, not the parent class it inherited from, and you need the first parameter to be a class. It can be set with a @classmethod on a method
  4. Now because we also want the string to be appended to BleepedString object, to also be bleeped, so we need to override __add__, so when string appended via + operator, will also be bleeped. Interestingly __iadd__ is not inherited, so I need to add __iadd__ method too, so that I can do +=
  5. Notice that inside __add__ I use bleep method like a normal method. That is the reason is set a @classmethod decorator in bleep method, I need the method to be available without need to be instantiated, while able to use it like a normal method in a object.
  6. Actually just override __add__ and __iadd__ is not enough to override the str behavior. Because the parent class str uses __repr__ to provide value to be used in a + and += operation, which is why I override __repr__ method, by replacing the return value to bleeped string.
  7. Then we need to override __str__ so that it return a bleeped string.

This is just a modified example what I used in my work, and have a very different and useful function. But it capture the same idea, overriding the default string behavior and return value.