Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8054533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:10:16+00:00 2026-06-05T08:10:16+00:00

I am building a realtime web application. I want to be able to send

  • 0

I am building a realtime web application. I want to be able to send broadcast messages from the server-side implementation of my python application.

Here is the setup:

  • socketio.js on the client-side
  • TornadIO2 server as Socket.IO server
  • python on the server-side (Django framework)

I can succesfully send socket.io messages from the client to the server. The server handles these and can send a response. In the following i will describe how i did that.

Current Setup and Code

First, we need to define a Connection which handles socket.io events:

class BaseConnection(tornadio2.SocketConnection):
    def on_message(self, message):
        pass

    # will be run if client uses socket.emit('connect', username)
    @event
    def connect(self, username):
        # send answer to client which will be handled by socket.on('log', function)
        self.emit('log', 'hello ' + username)

Starting the server is done by a Django management custom method:

class Command(BaseCommand):
    args = ''
    help = 'Starts the TornadIO2 server for handling socket.io connections'

    def handle(self, *args, **kwargs):
        autoreload.main(self.run, args, kwargs)

    def run(self, *args, **kwargs):
        port = settings.SOCKETIO_PORT

        router = tornadio2.TornadioRouter(BaseConnection)

        application = tornado.web.Application(
            router.urls,
            socket_io_port = port
        )

        print 'Starting socket.io server on port %s' % port
        server = SocketServer(application)

Very well, the server runs now. Let’s add the client code:

<script type="text/javascript">    
    var sio = io.connect('localhost:9000');

    sio.on('connect', function(data) {
        console.log('connected');
        sio.emit('connect', '{{ user.username }}');
    });

    sio.on('log', function(data) {
        console.log("log: " + data);
    });
</script>

Obviously, {{ user.username }} will be replaced by the username of the currently logged in user, in this example the username is “alp”.

Now, every time the page gets refreshed, the console output is:

connected
log: hello alp

Therefore, invoking messages and sending responses works. But now comes the tricky part.

Problems

The response “hello alp” is sent only to the invoker of the socket.io message. I want to broadcast a message to all connected clients, so that they can be informed in realtime if a new user joins the party (for example in a chat application).

So, here are my questions:

  1. How can i send a broadcast message to all connected clients?

  2. How can i send a broadcast message to multiple connected clients that are subscribed on a specific channel?

  3. How can i send a broadcast message anywhere in my python code (outside of the BaseConnection class)? Would this require some sort of Socket.IO client for python or is this builtin with TornadIO2?

All these broadcasts should be done in a reliable way, so i guess websockets are the best choice. But i am open to all good solutions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:10:17+00:00Added an answer on June 5, 2026 at 8:10 am

    I’ve recently written a very similar application on a similar setup, so I do have several insights.

    The proper way of doing what you need is to have a pub-sub backend. There’s only so much you can do with simple ConnectionHandlers. Eventually, handling class-level sets of connections starts to get ugly (not to mention buggy).

    Ideally, you’d want to use something like Redis, with async bindings to tornado (check out brukva). That way you don’t have to mess with registering clients to specific channels – Redis has all that out of the box.

    Essentially, you have something like this:

    class ConnectionHandler(SockJSConnection):
        def __init__(self, *args, **kwargs):
            super(ConnectionHandler, self).__init__(*args, **kwargs)
            self.client = brukva.Client()
            self.client.connect()
            self.client.subscribe('some_channel')
    
        def on_open(self, info):
            self.client.listen(self.on_chan_message)
    
        def on_message(self, msg):
            # this is a message broadcast from the client
            # handle it as necessary (this implementation ignores them)
            pass
    
        def on_chan_message(self, msg):
            # this is a message received from redis
            # send it to the client
            self.send(msg.body)
    
        def on_close(self):
            self.client.unsubscribe('text_stream')
            self.client.disconnect()
    

    Note that I used sockjs-tornado which I found to be much more stable than socket.io.

    Anyway, once you have this sort of setup, sending messages from any other client (such as Django, in your case) is as easy as opening a Redis connection (redis-py is a safe bet) and publishing a message:

    import redis
    r = redis.Redis()
    r.publish('text_channel', 'oh hai!')
    

    This answer turned out pretty long, so I went the extra mile and made a blog post out of it: http://blog.y3xz.com/blog/2012/06/08/a-modern-python-stack-for-a-real-time-web-application/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a multiuser realtime application with Google App Engine (Python) that would look
I am building a web application that has a real-time feed (similar to Facebook's
Building a new ASP.net application, and planning to separate DB, 'service' tier and Web/UI
I am building a website that has a system where users can send messages
I'm building a real-time web application As far as I know, the most popular
I was able to setup nginx as a message server for building a real-time
I'm building a web application where one of the features causes users to be
We are building a web-application and a significant portion of the project will be
I'm currently building a realtime application using Node. I'm using socket.io to power my
We are building a financial web application which requires a real time chart. Initially

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.