58

Is there any way to use Socket.IO http://socket.io/ with Django?

3
  • 3
    You're not being very specific. The basic answer is "no," since socket.io is a Node.js program with multi-browser support. On the other hand, it's perfectly possible to send events from Django to a Socket.io server over a local channel, and it's perfectly possible to modify table rows from Socket.io via DBSlayer to notify Django of changes, and it's even possible for both to leave traces in the client to communicate state between all three. But what are you trying to accomplish? Nov 16, 2010 at 5:00
  • 3
    I am asking does a server side django implementation exist for node.js in django.
    – User
    Nov 16, 2010 at 5:35
  • Redis can help, it solved my problem Feb 23, 2018 at 18:38

6 Answers 6

26

Sure you can!

Django itself arent asyncronous so you have to use a Socket.IO server in parallel with your normal django server, node.js isnt always a good choice but there exists others written in pure python.

here is a blog/tutorial that uses gevent as socket.io server.

http://codysoyland.com/2011/feb/6/evented-django-part-one-socketio-and-gevent/

For a similar solution that has a bit more history you can look at Orbited, (www.orbited.org)

2
  • 6
    There's even a Django application: django-socketio.
    – Denis
    Aug 15, 2011 at 6:04
  • 14
    @DenisBilenko, Note that that project's basically unmaintained and only works with a 6-year old version of socket.io that doesn't work with a lot of modern browsers. I learned this the hard way...
    – Cerin
    Feb 28, 2016 at 3:59
6

I am asking does a server side django implementation exist for node.js in django.

No. node.js is its own language running in its own interpreter. However if you are asking if there is a Django app which allows communicating with a Socket.IO client, then yes and no. No because no pre-made solution currently exists, and yes because all the parts you need to implement it already exist.

To implement the protocol with django, check out Django-Websocket for the backend websocket server, Ajax libraries from Dajax Project and Socket.IO-rack which is a ruby gem which implements the protocol and ruby is close enough in structure to django that you can get a lot of inspiration from it.

3
  • Yes I am asking if there is a django app which allows communicating with a Socket.IO client.
    – User
    Nov 16, 2010 at 23:21
  • No. There is no Socket.IO-django. but like i said, Django-websocket+Dajax implement all the server side parts you need to implement the protocol yourself.
    – Thomas
    Nov 18, 2010 at 2:50
  • 18
    Do NOT use django-websocket. I'm the author of it and explained quite a few times now that it is simply not possible to use websockets in a standard compliant way with django. See this answer and read the disclaimer on pypi. kthxbye Dec 8, 2010 at 9:07
2

Start here:

and here:

There are some Django examples as to how to get started.

It is based on Gevent, which implements a cooperative concurrency model. It's great coming from a request/response world, as it adds some callbacks and micro-threads without imposing it massively on your workflow.

Try it out :)

2

I think the best way to asyncronous communication with Django is have a node server listening in another port and use the api client of Socket.io. In this way, you aren't depend of the support of the modules for django and is very simple: Node listening the request from client, convert this request in a post request and send to Django for the port which listen Django. Is the best way i think.

server.js

var http = require('http');
var server = http.createServer().listen(3000);
var io = require('socket.io').listen(server);
var querystring = require('querystring');

io.on('connection',function(socket) {
    console.log('Connected to the client');
    socket.on('new comment',function(data) {

        console.log('Web--->Node');
        var values = querystring.stringify(data);
        console.log(values);

        var options = {
            hostname:'localhost',
            port:'8000',
            path:'/create-comment',
            method:'POST',
            headers:{
                'Content-Type':'application/x-www-form-urlencoded',
                'Content-Length':values.length
            }
        };

        var request = http.request(options, function(response) {
            response.setEncoding('utf8');
            response.on('data',function(data){
                //Here return django
                console.log('Django-->Node');
                io.emit('return comment',data);
            });
        });

        request.write(values);
        request.end();
    });
});

views.py

def trysock(request):
    print 'In tryshok'
    comments = Comment.objects.all()
    dic = {
              'name': 'User',
              'form': CommentForm(),
              'comments': comments
          }

    return render(request,'index.html',dic)

@csrf_exempt
def create_comment(request):
    print 'Django<---Node'
    Comment.objects.create(
            user = request.POST['user'],
            comment = request.POST['comment']
        )

    response = JsonResponse({'user' : request.POST['user'], 'comment' : request.POST['comment']})
    print response.content
    return HttpResponse(response.content)

index.html

<div class='col-md-12'>
    <div class='col-md-6'>
        <form method='POST'>
            {% csrf_token %}
            {{form.comment}}
            <button id='boton'>Comentar</button>
        </form>
    </div>

    <div id='comentarios' class='col-md-6'>
        {% for comment in comments %}
        <p>{{ comment.user }} - {{ comment.comment}}</p>
        {% endfor %}
    </div>
</div>
<!-- Fin Formulario comentarios -->

</div>
<script>
    var socket = io.connect('http://localhost:3000');
    console.log(socket);
    $('#boton').on('click', Comentar);

    function Comentar(e) {
        console.log('Comentar(e)')
        e.preventDefault();

        var datos = {
            user:"user",
            comment : 'comment'
        };

        socket.emit('nuevo comentario', datos);
        console.log('Enviando....: ' + datos.user + '-' + datos.comment);
    }

    socket.on('devolviendo comentario', function(data) {
        console.log('Recibiendo...');
        var dato = JSON.parse(data);
        $('#comentarios').prepend('<p>' + dato.user + '-' + dato.comment + '</p>')
    });
</script> 
1
  • How about the reserver? Django listen request from client, then Django send it to NodeJs? What is better?
    – KitKit
    Nov 23, 2018 at 4:32
1

For an example of using socket.io with django, you might want to look at django-serverpush. It integrates django with socket.io as the transport and tornado/tornandio2 as the async server instead of node.js

https://github.com/hamax/django-serverpush

Other have used django with node.js+socket.io with rabbitMQ as the message queue bridge between the two. There was a talk, "Real-Time Django" at djangocon 2011 which described usng this approach for large real-time (award-show-type) applications.

0

Guys there is no solid support for socket.io with django... there is how ever easy toy program support g-event socket.io support was dropped for the last year as well, django-socket.io is also abandoned. If you want to build a simple toy program using django-socket.io and or g-event socket.io sure, but anything that scales is unlikely "https://github.com/ryesoft/gevent-socketio" this is experimental.

Issues with more than 1 worker with gunicorn.... https://github.com/abourget/gevent-socketio/issues/112

The work around is this commit. https://github.com/getpenelope/gevent-socketio/commit/6c80c17705c9a1b3adcb0f99abde1c4d369c337b

I thought it would throw it out there just incase someone was trying to use this in a production environment like my self.

Not the answer you're looking for? Browse other questions tagged or ask your own question.