Note that there are some explanatory texts on larger screens.

plurals
  1. POpython Socket.IO client for sending broadcast messages to TornadIO2 server
    text
    copied!<p>I am building a realtime web application. <strong>I want to be able to send broadcast messages from the server-side implementation of my python application.</strong></p> <p>Here is the setup:</p> <ul> <li><strong><a href="http://socket.io/" rel="noreferrer">socketio.js</a></strong> on the client-side</li> <li><strong><a href="https://github.com/mrjoes/tornadio2" rel="noreferrer">TornadIO2</a></strong> server as Socket.IO server</li> <li><strong>python</strong> on the server-side (<strong><a href="https://www.djangoproject.com/" rel="noreferrer">Django</a></strong> framework)</li> </ul> <p>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.</p> <h2>Current Setup and Code</h2> <p>First, we need to define a Connection which handles socket.io events:</p> <pre><code>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) </code></pre> <p>Starting the server is done by a Django management custom method:</p> <pre><code>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) </code></pre> <p>Very well, the server runs now. Let's add the client code:</p> <pre><code>&lt;script type="text/javascript"&gt; 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); }); &lt;/script&gt; </code></pre> <p>Obviously, <code>{{ user.username }}</code> will be replaced by the username of the currently logged in user, in this example the username is "alp".</p> <p>Now, every time the page gets refreshed, the console output is:</p> <pre><code>connected log: hello alp </code></pre> <p>Therefore, invoking messages and sending responses works. <em>But now comes the tricky part.</em></p> <h2>Problems</h2> <p>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).</p> <p>So, here are my questions:</p> <ol> <li><p>How can i send a broadcast message to all connected clients?</p></li> <li><p>How can i send a broadcast message to multiple connected clients that are subscribed on a specific channel?</p></li> <li><p>How can i send a broadcast message anywhere in my python code (outside of the <code>BaseConnection</code> class)? Would this require some sort of Socket.IO client for python or is this builtin with TornadIO2?</p></li> </ol> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload