{ "info": { "author": "Alessandro Molina", "author_email": "alessandro.molina@axant.it", "bugtrack_url": null, "classifiers": [ "Environment :: Web Environment", "Intended Audience :: Developers", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7" ], "description": "About tgext.socketio\n-------------------------\n\n.. image:: https://travis-ci.org/amol-/tgext.socketio.png\n :target: https://travis-ci.org/amol-/tgext.socketio\n\n.. image:: https://coveralls.io/repos/amol-/tgext.socketio/badge.png\n :target: https://coveralls.io/r/amol-/tgext.socketio\n\n.. image:: https://img.shields.io/pypi/pyversions/tgext.socketio.svg\n :target: https://pypi.python.org/pypi/tgext.socketio\n\ntgext.socketio provides an easy way to implement SocketIO in TurboGears2.\nSocketIO support is provided by gevent-socketio and specific SocketIO server\nbased on gevent is provided.\n\nInstalling\n-------------------------------\n\ntgextsocketio can be installed from pypi::\n\n pip install tgext.socketio\n\nRunning\n------------------------------\n\nSocketIO support requires a custom GEvent based HTTP server to work\ncorrectly, remember to edit your ``development.ini`` and add::\n\n # SOCKETIO doesn't work with debug mode\n debug = false\n\n [server:main]\n use = egg:tgext.socketio#socketio\n socketio_resource = socketio\n\n host = 127.0.0.1\n port = 8080\n\n``tgext.socketio#socketio`` is the server provided by tgext.socketio\nto correctly support SocketIO, while ``socketio_resource`` is the\npath of your ``SocketIOController`` class.\n\nBy default ``socketio`` is used so you can mount a ``SocketIOController``\nsubclass under the ``RootController`` or you can mount it in any other location\nand change your ``socketio_resource`` option.\n\nUsing SocketIOController\n----------------------------------\n\n``SocketIOController`` works like any other TurboGears controller but supports\nserving ``SocketIOTGNamespace`` as sub controllers. ``SocketIOController`` can\nalso behave like any other TG Controller and provide normal web pages.\n\n``SocketIOTGNamespace`` are subclasses of ``socketio.namespace.Basenamespace`` with\nadditional TurboGears features like ``@validate`` and ``@require`` support.\nPlease keep in mind that ``SocketIOTGNamespace`` requires at least TurboGears **2.3.1**,\nif you want to use a previous TurboGears version you can still use ``SocketIOController``\nwith plain ``socketio.namespace.Basenamespace`` classes.\n\nUsage Example\n=====================\n\nThe following is a very simple example that handles two type of events\n``ping`` and ``fireball`` and response on real time to those::\n\n from tgext.socketio import SocketIOTGNamespace, SocketIOController\n\n class PingPong(SocketIOTGNamespace):\n def on_ping(self, attack):\n if attack['type'] == 'fireball':\n for i in range(10):\n self.emit('pong',{'sound':'bang!'})\n else:\n self.emit('pong',{'sound':'pong'})\n\n\n class SocketIO(SocketIOController):\n pingpong = PingPong\n\n @expose()\n def page(self, **kw):\n return '''\n \n
\n \n \n \n \n \n \n \n '''\n\n class RootController(BaseController):\n socketio = SocketIO()\n\nKeep in mind that the ``SocketIOController`` must be mounted in the same location\nspecified by ``socketio_resource`` property in your configuration file.\n\nUsing Validators and Requirements\n=====================================\n\n``SocketIOTGNamespace`` also supports using ``@validate`` and\n``@require`` TurboGears decorators.\n\nThe same example can be changed to provide validation and\npermission checks with a few lines of code::\n\n from tg import validate, require, predicates\n from tg.validation import TGValidationError\n import random\n\n class NoFireBallValidator(object):\n def to_python(self, value, *args, **kw):\n type_ = value['type']\n if type_ == 'auto':\n return {'type': random.choice(['ping', 'fireball'])}\n elif type_ == 'error':\n raise TGValidationError('Got an error!')\n\n return value\n\n\n class PingPong(SocketIOTGNamespace):\n @require(predicates.not_anonymous())\n @validate({'attack': NoFireBallValidator()})\n def on_ping(self, attack):\n if attack['type'] == 'fireball':\n for i in range(10):\n self.emit('pong',{'sound':'bang!'})\n else:\n self.emit('pong',{'sound':'pong'})\n\n\nPubSub Support\n---------------------------------------\n\n``tgext.socketio`` has builtin support for PubSub paradigm based\non ``anypubsub`` library. If you want to use PubSub you should install\nanypubsub through ``pip install anypubsub`` or add it to your project\ndependencies.\n\nPubSub support works by subclassing from ``tgext.socketio.pubsub.PubSubTGNamespace``\nthis special namespace permits clients to subscribe to channels using\n``socket.emit('subscribe', 'channel_name')`` from the javascript interface.\n\nWhenever an user subscribes to a channel, the PubSubTGNamespace subclass will receive\na call for ``subscribe_channelname`` method, which can return if the user is permitted\nto subscribe to the given channel or not (``@require`` decorator can be used).\nThe ``subscribe_channelname`` method can also return a different channel name if you want\nto specify a subchannel.\n\nFor each message published on the subscribed channel PubSubTGNamespace will emit a\n``pubblication`` event, which can be trapped by the socket.io client to perform required\nactions.\n\nPublishing a message will be possible through ``PubSubTGNamespace.publish``.\n\nYou can see a simple example providing a real time chat implemented on redis backend\nin `examples/chat.py