This page applies to an older version of Orbited (Version 0.3) For info on the current version see the main documentation.
Goal
By the end of this tutorial, you will have created a comet-based chat application using Orbited and mod_python.
Prerequisites
1. Orbited makes it easy to write comet-based web applications
To install, please see the Installation tutorial
2. PyOrbited lets you use Python with Orbited
To install, use easy_install.
easy_install pyorbited
For more information about PyOrbited, see the tutorial Using Python with Orbited.
3. mod_python embeds the Python interpreter in your Apache server.
Python-based web applications run significantly faster with mod_python. After downloading the module, you need to configure your Apache server. For a refresher on using mod_python, see the following tutorial.
Daemon
1. Configure daemon
Create a file called orbited.cfg in the directory from which you will start Orbited and add these contents:
[global] proxy.enabled = 1 proxy.keepalive = 0 [proxy] /orbited -> ORBITED / -> http://localhost
The configuration file tells the proxy to redirect /orbited requests to our Orbited daemon and redirect all other requests to the Apache server. If your Apache server is listening to a different port such as 8080, you need to specify the port in the configuration file:
/ -> http://localhost:8080
2. Start daemon
Change to the directory containing the configuration file orbited.cfg that you created in step one. Start the daemon by typing the following command:
orbited
3. Access daemon through proxy
The orbited proxy defaults to listening on port 8000, so make sure that all requests to the Orbited daemon specify port 8000.
Python
Create a file called chat.py in your web directory and add the following content. Note that we retrieve orbited.js from the Orbited daemon. You should set localhost:8000 to the address and port of your Orbited daemon.
# Get a reference to the Orbited daemon
from pyorbited.simple import Client
orbited = Client()
orbited.connect()
# Initialize a list of chat users
users = []
# Return a list of userKeys for use with orbited.event()
def __getUserKeys():
return ['%s, %s, /orbited' % (user, session) for user, session in users]
# Add the user to the list of users
def join(req):
user = req.form.getfirst('user')
session = req.form.getfirst('session','0')
if (user, session) not in users:
users.append((user, session))
orbited.event(__getUserKeys(), '<b>%s joined</b>' % user)
# Send the message to all users
def message(req):
user = req.form.getfirst('user')
session = req.form.getfirst('session', '0')
message = req.form.getfirst('message')
orbited.event(__getUserKeys(), '<b>%s</b> %s' % (user, message))
# Set localhost:8000 to the address and port of your Orbited daemon
html_chat = """\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script src=http://localhost:8000/_/orbited.js></script>
<script src=chat.js></script>
</head>
<body>
<table>
<tr>
<td>Nickname</td>
<td><input id=nickname></td>
<td><input type=button value=Connect onclick=connect()></td>
</tr>
<tr>
<td>Message</td>
<td><input id=chat></td>
<td><input type=submit value=Send onclick=sendMessage()></td>
</tr>
</table>
<div id=box></div>
</body>
</html>"""
# Return the main chat page
def index(req):
return html_chat
Javascript
Create a file called chat.js in your web directory and add the following content:
// Prevent caching in Internet Explorer
var ie_nocache = 1;
// Get request object
function create_xhr() {
return window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
}
// Connect to the Orbited daemon and specify chat_event as the callback
function connect() {
var name = document.getElementById('nickname').value;
Orbited.connect(chat_event, name, '/orbited', '0');
join(name);
}
// The Orbited daemon will call this method when this user receives an event
function chat_event(data) {
var chat_box = document.getElementById('box');
var div = window.parent.document.createElement('div');
div.className = 'event';
div.innerHTML = data;
chat_box.appendChild(div);
}
// Adds the user
function join(user) {
var xhr = create_xhr();
xhr.open('GET', '/chat/join?user=' + user, true);
xhr.send(null);
// Disable connect button
document.getElementById('nickname').disabled = true;
document.getElementById('button_connect').disabled = true;
}
// Sends the message
function sendMessage() {
ie_nocache += 1;
var xhr = create_xhr();
var msg = document.getElementById('chat').value;
var name = document.getElementById('nickname').value;
xhr.open('GET', '/chat/message?ie_nocache=' + ie_nocache + '&user=' + name + '&message=' + msg, true);
xhr.send(null);
}
Browser
Make sure that you have started the Orbited daemon and point your browser to http://localhost:8000/chat and open a second browser window to http://127.0.0.1:8000/chat. Type a nickname, click Connect. Then type a message and click Send.