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.
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.
Note: You can use either https or wss (respectively, http or ws ).
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With