Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Websockets with Pyramid and socket.io?

I'm trying to create a simple WebSocket application using 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 for the connection, but it falls back to XHR-polling. How can I fix it?

Thanks in advance, Ivan.

like image 584
Ivan Gromov Avatar asked Mar 16 '12 13:03

Ivan Gromov


People also ask

Should I use Socket.IO or WebSockets?

Both WebSocket vs Socket.io are popular choices in the market; let us discuss some of the major Difference Between WebSocket vs Socket.io: It provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback.

Does Socket.IO use WSS?

Note: You can use either https or wss (respectively, http or ws ).

Does Socket.IO use WebRTC?

Adding the ability to allow others to stream their video Now it's time to use Socket.io and PeerJS. For those who don't know Socket.io allows us to do real-time communication. and PeerJS allow us to implement WebRTC.

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).


2 Answers

You probably want to look at the latest release of gevent-socketio, and its documentation at http://gevent-socketio.readthedocs.org/

A major overhaul was done at the PyCon 2012 sprints, by John Anderson, Sébastien Béal and myself.

like image 105
abourget Avatar answered Oct 06 '22 00:10

abourget


You may also have a look at pyramid_sockjs. It integrates well with Pyramid and uses sockjs that fulfills the same role of socket.io and is arguably simpler to understand.

like image 33
Danny Navarro Avatar answered Oct 06 '22 00:10

Danny Navarro