This tutorial will describe how to set up an introductory “real time chat” application using the Orbited framework and TurboGears. Real-time web-sites are capable of far more than simple chat applications, but chat is the common “Hello World” of real-time developers.
To install the Orbited framework in your VirtualEnv we need to install the Orbited 0.7.10+ and Twisted 9.0+ packages. This will also pull in the morbidq package.
(tgenv)$ easy_install twisted orbited
MorbidQ provides an easily configured message broker/queue which is not intended for large-scale production use. It uses the simple STOMP protocol. If you want to stick with STOMP as you scale up, you can explore the other STOMP servers available.
To configure MorbidQ and Orbited, you need a config file. Something like the following, which we will save as “chat.ini” in our “rtchat” project’s directory.
You can now run Orbited with the embedded MorbidQ queue with the following command:
(tg2env)$ orbited --config=chat.ini
You should see messages telling you that Orbited/MorbidQ is listening on the defined ports. You can hit CTRL-C to stop the server, though we’ll want to use it in a moment, so you’ll likely want to leave it running and start another console.
We are going to be very simplistic with our chat widget in our first attempt. We’ll simply dump the text which is sent to the server into a div node. The view looks like this:
<?python # we pull some values out of TurboGears config-file, with defaults # for our tutorial settings. from simplejson import dumps as d orbited_server = config.get( 'orbited_server', 'localhost' ) orbited_port = config.get( 'orbited_port', 9000 ) stomp_server = config.get( 'stomp_server', 'localhost' ) stomp_port = config.get( 'stomp_port', 61613 ) orbited_files = 'http://%s:%s/static'%( orbited_server, orbited_port ) ?> <div id="chat"> <h2>Real-time Chat</h2> <div class="chat-trace"> </div> <div class="chat-entry"> Chat: <input class="chatter" /> <button class="chat-trigger">Send</button> </div> </div>
The chat client we show here is extremely simplistic. It is intended to show you the minimum required to get messages flowing across the MorbidQ server.
You should now be able to start your TurboGears server, browse to http://localhost:8080 and start chatting. Your messages should show up in the chat-trace DIV as you enter them.
You will immediately notice problems with the chat system, some obvious enhancements:
There are a number of callbacks of the STOMP object that you may wish to override to perform basic configuration and the like:
The methods to control the STOMP object are: