Skip to main content

Gevent-based Socket.IO pyramid integration and helpers

Project description

Gevent-based Socket.IO integration for Pyramid (and WSGI frameworks)

To use the server, either run:

socketio-serve development.ini
socketio-serve-reload development.ini

or tweak your <code>[server:main]</code> section in your development.ini to:

use = egg:pyramid_socketio#sioserver_patched resource =
host =
port = 6555

otherwise, follow instructions given for <code>pastegevent</code>.

Simple in-Pyramid usage:

### somewhere in a Pyramid view:
from pyramid_socketio import SocketIOContext, socketio_manage

class ConnectIOContext(SocketIOContext):
"""Starting context, which will go one side or the other"""
def msg_connect(self, msg):
if msg.get('context') not in contexts:"error", error="unknown_connect_context",
msg="You asked for a context that doesn't exist"))
# Waiting for a msg such as: {'type': connect', 'context': 'section'}
newctx = self.switch(contexts[msg['context']])
if hasattr(newctx, 'startup'):
# Returning a new IOContext switches the WebSocket context, and will
# call this context's methods for next incoming messages.
return newctx

def msg_login(self, msg):
# Do the login, then wait for the next connect
self.request.session.user_id = 123
print "Logged in, session created and"

class SectionIOContext(SocketIOContext):
def startup(self, connect_msg):
print "Started the section context"
self.my_id = connect_msg['section_id']
# TODO: make sure we don't leak Sessions from MongoDB!
from intr.models import mdb # can't import globally, because of Pyramid
self.db = mdb
self.conn = BrokerConnection("localhost", "guest", "guest", "/")
self.chan =
self.queue = Queue("session-%s" %,
durable=False, exclusive=True,
routing_key="section.%s" % self.my_id)

self.producer = Producer(self.chan, exchange=my_exchange,
routing_key="section.%s" % self.my_id)
self.consumer = Consumer(self.chan, [self.queue])

# Do we need this ? Please fix the session instead, have a new one
# init'd for each incoming msg, or when calling save(), re-create a new
# SessionObject.
request = self.request
self.user = request.session['user']
self.temporary = request.session['temporary']
self.user_id = request.session['user_id']

def consume_queue_message(self, body, message):
"""Callback when receiving anew message from Message Queue"""
# Do something when received :)
print "Received message from queue:",, body

def queue_recv(self):
"""Wait for messages from Queue"""
# consume queue...
while True:
if not

# Socket messages
def msg_memorize(self, msg):
# "memorized" is the 'type' attribute, any other kwarg added will be
# added to the JSON object.
self.msg("memorized", some="thing")

def msg_forget(self, msg):
self.error("error_code", "Error message")

def msg_change_privacy(self, msg):

def msg_get_members(self, msg):

def msg_enter_game(self, msg):
return self.switch(SomeOtherIOContext)

contexts = {'section': SectionIOContext,
'somewhereelse': SocketIOContext,

# SOCKET.IO implementation
def socket_io(request):
"""Deal with the SocketIO protocol, using SocketIOContext objects"""
# Offload management to the pyramid_socketio module

retval = socketio_manage(ConnectIOContext(request))
#print "socketio_manage ended"
return Response(retval)

#### Inside for your Pyramid application:
def main(..):
config.add_static_view('', 'intr:static')
config.add_route('socket_io', '*remaining')

In the routes and view configurations, '' is the "resource" specified either in the server (under [server:main], key=resource), and is by default "". This is pretty much a standard..



- CHANGES and README added to description.


- Help updated


- Initial release
- Support for paster server plugin
- Provide with command-line servers
- socketio-serve
- socketio-serve-reload

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pyramid_socketio, version 0.3
Filename, size File type Python version Upload date Hashes
Filename, size pyramid_socketio-0.3.tar.gz (6.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page