How do you send a broadcast message inside an arbitrary Django request?

32 views
Skip to first unread message

Jeff Tchang

unread,
Jul 2, 2014, 2:02:30 PM7/2/14
to gevent-...@googlegroups.com
I must be missing a fundamental piece of the socketio picture:

Here is a normal request:

def broadcast(request):
    print request.server
    return HttpResponse(status=200)

I'd like to broadcast out a message to everyone connected.


This code lives inside a namespace:


    def broadcast_msg(self, msg):
        """
        This is sent to all in the sockets in this particular Namespace,
        including itself.
        """
        pkt = dict(type="event",
                   name='msg',
                   args=str(msg),
                   endpoint=self.ns_name)

        for sessid, socket in self.socket.server.sockets.iteritems():
            socket.send_packet(pkt)


I thought I could just use 

        for sessid, socket in self.socket.server.sockets.iteritems():
            socket.send_packet(pkt)

but I can't seem to find where socket is set. I tried looking inside request.environ['SOCKETIO'] but it is not there because the WSGIServer does not set it for non socket io requests.
Then I started digging into gevent and seeing if I could find all greenlets and maybe pull the socket pointer from them but that got me nowhere.

Where is the handle to the sockets?
Reply all
Reply to author
Forward
0 new messages