How to use websockets with pyramid and socket.io?

I am trying to create a simple WebSocket application using the Pyramid and socket.io frameworks. Server Side Code:

from pyramid.response import Response
from pyramid_socketio.io import SocketIOContext, socketio_manage
import gevent

def includeme(config):
    '''
    This method is called on the application startup.
    '''
    config.add_route('socket.io', 'socket.io/*remaining')

class ConnectIOContext(SocketIOContext):
    # self.io is the Socket.IO socket
    # self.request is the request
    def msg_connect(self, msg):
        print "Connect message received", msg
        self.msg("connected", hello="world")

# Socket.IO implementation
@view_config(route_name="socket.io")
def socketio_service(request):
    print "Socket.IO request running"
    print request
    retval = socketio_manage(ConnectIOContext(request))
    return Response(retval)

Client Code:

<script>
    var socket = null;
    $(document).ready(function() {
        socket = new io.Socket(null, null);
        socket.on('connect', function() {
        console.log("Connected");
        socket.send({type: "connect", userid: 123});
    });
    socket.on('message', function(obj) {
        console.log("Message received");
        console.log("Message", JSON.stringify(obj));
        if (obj.type == "some") {
            console.log("do some");
        }
    });
    socket.on('error', function(obj) {
        console.log("Error", JSON.stringify(obj));
    });
    socket.on('disconnect', function() {
        console.log("Disconnected");
    });

    console.log("Connecting...");
    socket.connect();
});
</script>  

I need this code to use web sockets to connect, but it goes back to the XHR poll. How can i fix this?

Thank you Ivan.

+5
source share
2 answers

You will probably want to see the latest version of gevent-socketio and its documentation at http://gevent-socketio.readthedocs.org/

Overhaul was done at the 2012 PyCon Sprint by John Anderson, Sebastian Beal and me.

+8
source

pyramid_sockjs. Pyramid sockjs, socket.io , , .

+2

All Articles