Installing pywebsocket and samples on Fedora
I've been playing a bit with a Python implementation of WebSockets over the past couple of days, and whilst the documentation does cover everything, it's a bit uncentralized. Here are some of my notes about getting it up and running on Fedora 11 and 12...
Prerequisites
Browsers
Support for WebSockets across all browsers is still an issue, but I've tested this successfully with the following browsers as of :
- Firefox 4.0 nightly build (Fedora, WinXP)
- WebKit nightly (Fedora)
- SRWare Iron 6.0.475 (WinXP)
- Safari 5.0.2 (WinXP)
- SRWare Iron 5.0.382 (WinXP)
- Firefox 3.6.10 (WinXP)
The protocol has changed over time, I believe Opera and older versions of Chrome only support an older version, which can be made to work with this library according to the documentation, but which I haven't tried personally.
As I understand it, there are no plans for WebSockets to be supported in IE9 - surprise, surprise...
Server side packages
Although the download includes a standalone server, I've only used the module within Apache.
You need mod_python (Fedora package is similarly named) for mod_pywebsocket.
The Ruby and Wakachi sample applications use MeCab, a library for Japanese language handling. The Fedora packages needed are for that are:
- python-mecab
- mecab
- mecab-ipadic
- mecab-ipadic-EUCJP
mod_pywebsocket
The project page is
http://code.google.com/p/pywebsocket/.
The source can be checked out from Subversion via:
svn checkout http://pywebsocket.googlecode.com/svn/trunk/ pywebsocket-read-only
To build and install the library, do the following:
cd pywebsocket-read-only/src
python setup.py build
su
{enter root password}
python setup.py install
{exit root session}
Verify that the module has been installed within your PYTHONPATH
by importing it from an interactive Python session without errors:
python
import mod_pywebsocket
{exit python session}
For the purposes of these tests, I created a new Apache configuration
file, serving on port 8003. Create a file
/etc/httpd/conf.d/websocket_test.conf
with the following content, modifying the highlighted directory paths as appropriate:
# An Apache config for the testing the pywebsocket samples
# http://code.google.com/p/websocket-sample/
# Put this in /etc/httpd/conf.d and restart Apache
# This line should be unnecessary
# PythonPath "sys.path+['/websock_lib']"
<IfModule python_module>
PythonOption mod_pywebsocket.handler_root /proj/3rdparty/pywebsocket/websocket-sample-read-only/python
PythonHeaderParserHandler mod_pywebsocket.headerparserhandler
</IfModule>
Listen 8003
NameVirtualHost *:8003
<VirtualHost *:8003>
DocumentRoot /proj/3rdparty/pywebsocket/websocket-sample-read-only/html
Options Indexes MultiViews FollowSymLinks
ServerName pywebsocket.example.com
ErrorLog logs/pywebsocket_error.log
CustomLog logs/pywebsocket_access.log common
ErrorDocument 404 /error.html
</VirtualHost>
Restarting Apache will pick up this new configuration.
Samples
The sample applications are a separate project located at
http://code.google.com/p/websocket-sample/. Checking
out the code can be done via
svn checkout http://websocket-sample.googlecode.com/svn/trunk/ websocket-sample-read-only
The count and litechat sample applications use local files to store state.
These files need to be made writable by the account that runs Apache:
- python/pub/litechat/messages
- python/pub/count/count
These files need to be updated to reflect the location of the aforementioned files:
- python/pub/litechat/do_wsh.py
- python/pub/count/do_wsh.py
diff do_wsh.py do_wsh.py.ORIG
86c86
< file = '/proj/3rdparty/pywebsocket/websocket-sample-read-only/python/pub/count/count'
---
> file = '/home/komasshu/websock_handler/pub/count/count'
At this point you should hopefully be good to try the sample applications...
count
Shows the number of connected clients
litechat
A simple chat application where you enter a message to broadcast to the other connected clients.
ruby and wakachi
These are a bit more complicated to understand, as they assume you know something about how the Japanese language works.
Japanese is normally written without any spaces between words. The wakachi demo processes some sample text, putting spaces between the words.
Japanese also has 3 alphabets. Two of them are syllabic in nature - the symbol/ideograph directly indicates how it should be pronounced. However kanji can have multiple ways of being read. Ruby characters are kana which can be printed alongside the kanji in a smaller font to indicate the correct pronunciation. The ruby demo takes the same regular Japanese text, processing it to add these ruby characters where needed.
I should point out that I wasn't able to get the XHR functionality of these two applications to work - the Apache logs showed 404 errors. As the point of these applications is to show WebSockets in action, I didn't bother investigating what the problem was with the XHR stuff.