Pedro Assunção

Websockets tutorial/example with pywebsocket

As everyone already knows, Google Chrome now supports websockets. In essence, this allows you to keep a connection open with a webserver indefinitely (analogous to typical sockets) and send data bi-directionally. Unfortunately Chrome is the only browser currently supporting this, but I’m pretty sure this will change.

So I decided to give this a try and experiment a bit with it. This is my step by step process on getting a web page opening a websocket to a server and receiving the server’s date and time every second. It is based on an article by Joe Armstrong, though he uses Erlang for the server, while I decided to follow the easy road and use Google’s pywebsocket – an apache module (uses mod_python) that allows you to create handlers for websocket connections in a easy-to-use fashion. The code also contains a way to start a standalone server (i.e. not requiring apache) for testing purposes.

So here are the steps I took to get this working:

1. Create a web page

This is the code I borrowed from Joe, though slightly modified to fit my purposes (it requires jquery, by the way):

<html>

<head>

<script src="jquery-1.3.2.min.js"></script>
<script>

$(document).ready(function(){

var ws;

if ("WebSocket" in window) {
debug("Horray you have web sockets. Trying to connect...");
ws = new WebSocket("ws://localhost:9998/echo");

ws.onopen = function() {
// Web Socket is connected. You can send data by send() method.
debug("connected...");
ws.send("hello from the browser");
ws.send("more from browser");
};

run = function() {
var val=$("#i1").val(); // read the entry
$("#i1").val("");       // and clear it
ws.send(val);           // tell erlang
return true;            // must do this
};

ws.onmessage = function (evt)
{
//alert(evt.data);
var data = evt.data;
var i = data.indexOf("!");
var tag = data.slice(0,i);
var val = data.slice(i+1);
$("#" + tag).html(val);
};

ws.onclose = function()
{
debug(" socket closed");
};
} else {
alert("You have no web sockets");
};

function debug(str){
$("#debug").append("<p>" +  str);
};

});
</script>

</head>

<body>

<h1>Interaction experiment</h1>

<h2>Debug</h2>
<div id="debug"></div>

<fieldset>
<legend>Clock</legend>
<div id="clock">I am a clock</div>
</fieldset>

</body>

</html>

2. Download and install pywebsocket

Checkout the code with

svn checkout http://pywebsocket.googlecode.com/svn/trunk/ pywebsocket-read-only

Then do python setup.py build and sudo python setup.py install inside the src folder. This will install it into your python environment.

3. Being lazy, means we will change an example handler

The way pywebsocket works is delegating the connections to something they call handlers. In the pywebsocket-read-only/src/example folder you will find a file named echo_wsh.py. They have this convention where handlers are named <entry_point>_wsh.py. This means that when you later call (from your web page) the url http://localhost:9998/echo the server will delegate the processing of that connection to that file.

I modified that file to something like this:

# Copyright 2009, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from mod_pywebsocket import msgutil
from datetime import datetime
import time

_GOODBYE_MESSAGE = 'Goodbye'

def web_socket_do_extra_handshake(request):
 print 'Connected.'
 pass  # Always accept.

def web_socket_transfer_data(request):
 while True:
 time.sleep(1)
 date = datetime.now()
 #try:
 #    line = msgutil.receive_message(request)
 #except Exception, e:
 #    print 'Foi com os porcos'
 #    raise e
 #print 'Got something: %s' % line
 #msgutil.send_message(request, line)
 msgutil.send_message(request, 'clock!%s' % date)
 #if line == _GOODBYE_MESSAGE:
 #    return

So basically whenever a new connection is made to this entry point, a call to web_socket_do_extra_handshake is made. After that, web_socket_transfer_data is called and it’s your responsibility to create the loop that receives messages and handles the flow (as you can see in the commented lines). I don’t care about that right now, since I only want to push the date and time to the client every second.

3. Start the standalone server

Go to the pywebsocket-read-only/src/mod_pywebsocket folder and run the following command:

sudo python standalone.py -p 9998 -w ../example/

This will start the server in port 9998 and use the handlers directory specified by the -w option. That is where our echo_wsh.py lives.

4. Test it :D

So now open your browser (remember, only chrome supports websockets currently) and open the html file your created in the beginning. Voilá, server’s date and time every second in the clock div.

Related:

  1. My small pywebsocket tutorial … My small pywebsocket tutorial gets a lot of hits these...
  2. Java Web Start (jnlp) simple example One of my current projects requires me to deliver a...


Categorised as: code snipplets, computers, experiments, software development, tutorials


26 Comments

  1. Nick says:

    Do you know if there's any way to get a list of all websocket connections currently on the pywebsocket server? I'm trying to have all connected users have access to one file, and when the file is updated, I'd like the server to notify all users of the update. This means I need that list!! haha

    thank you :)

    • nocivus says:

      Hi Nick,

      I would suggest looking into what is available inside the "request" object that is passed to the web_socket_transfer_data function. You can do that by adding "print dir(request)" as the first line of the method. I'm sure you might find interesting information in there.

      Even if you don't, i can imagine you could maintain a list of "request" objects – global to that python module – and use that to communicate with all clients that have ever sent anything to the server.

      Hope it helps.

  2. goldalworming says:

    when i tried the code, i got an error
    first :
    DeprecationWarning: the md5 module is deprecated; use hashlib instead
    from md5 import md5

    [2010-11-24 00:18:35,082] [ERROR] mod_pywebsocket.handshake: Handshake error: Header Upgrade is not defined

    how to fix it??

    • nocivus says:

      My quick guess is that the hashlib library will now provide the md5 (and other algorithms') methods. Do a google search on hashlib and find the appropriate replacement calls for what is being done with the md5 library.

      • goldalworming says:

        yes, i have change it before with from hashlib import md5

        then the
        [2010-11-24 14:32:44,273] [ERROR] mod_pywebsocket.handshake: Handshake error: Header Upgrade is not defined erorr appear

        then i use
        sudo python standalone.py –allow-draft75 -p 9998 -w …..
        (from http://code.google.com/p/websocket-sample/updates… )

        but a new error shown
        [2010-11-24 14:49:24,517] [ERROR] mod_pywebsocket.handshake: Handshake error: Sec-WebSocket-Key1 not found
        [2010-11-24 14:49:24,517] [WARNING] mod_pywebsocket.handshake: fallback to old protocol
        [2010-11-24 14:49:24,517] [WARNING] root: mod_pywebsocket: No handler for: '/pub/litechat/do'

        do you have any idea??

        • nocivus says:

          No idea, sorry. And unfortunately i don't have time to dig into it. Been super busy lately with other projects :(

        • Tautvis says:

          I think you have wrong python version. Try it with 2.6.5

        • nervebox says:

          I've been trying to get around the same error message. I've checked every aspect of my Apache conf many times already. And I still get this error when trying to connect the websocket in the litechat.html example.

          [Sun Nov 20 17:08:47 2011] [info] [client 127.0.0.1] No handler for resource: '/pub/litechat/do'
          [Sun Nov 20 17:08:47 2011] [info] [client 127.0.0.1] Fallback to Apache
          [Sun Nov 20 17:08:47 2011] [error] [client 127.0.0.1] File does not exist:

          Did you find a way around this?

          • nocivus says:

            Sorry Andy, unfortunately i haven't had time to explore pywebsocket further :( Hope you manage to find a solution! Cheers,–Pedro Assunção <p style=”color: #A0A0A8;”>

  3. Ryan Feeney says:

    Useful tutorial, but the code as listed here needed an update before it would work.

    in echo_wsh.py:

    def web_socket_transfer_data(request):
    while True:
    time.sleep(1)
    date = datetime.now()
    msgutil.send_message(request, "clock!"+date.isoformat())

    The function call which send's the date back to the browser was missing, also the indent was not correct for the while true loop.

    • nocivus says:

      I did try it myself, when i wrote the post but it's probable that – in the meantime – some things have changed.

      Thanks for the update :)

  4. mtjop says:

    Thanks for the tutorial. I have a feeling these websockets are gonna be a big thing…I hear safari now supports it and firefox will have a beta version in 4.0..Im just wondering how would this work on a server running just php?

    • nocivus says:

      No problem. And i agree with your statements; i think it will at least be a transition technology towards a more real-time web.

  5. Ryan says:

    Awesome, it works! Thanks! =D

  6. [...] implementation there are some nice code out there which will get you up and running with both Python and Node.js as backend tweetmeme_url = 'http://popdevelop.com/?p=92'; 0 [...]

  7. Social comments and analytics for this post…

    This post was mentioned on Twitter by nocivus: Websockets tutorial/example with pywebsocket http://diffract.me/2009/12/websockets-tutorialexample-with-pywebsocket/...

  8. [...] This post was Twitted by kuryaki [...]

  9. Oskar says:

    Great post BTW!

  10. Axe says:

    What changes should be made in the settings of Apache?

  11. Devraj says:

    Depending on your version of Python, in my case 2.6 on OS X.

    datetime module in python works a bit different. I had to change

    date = datetime.now()

    to

    date = datetime.datetime.today()

    • That’s odd, because this example from the python 2.6.4 shows differently :)

      >>> from datetime import datetime, date, time
      >>> # Using datetime.combine()
      >>> d = date(2005, 7, 14)
      >>> t = time(12, 30)
      >>> datetime.combine(d, t)
      datetime.datetime(2005, 7, 14, 12, 30)
      >>> # Using datetime.now() or datetime.utcnow()
      >>> datetime.now()
      datetime.datetime(2007, 12, 6, 16, 29, 43, 79043) # GMT +1

      Check it out, here: http://docs.python.org/library/datetime.html#module-datetime

    • Oskar says:

      That only depends on how you import datetime and has nothing to do with python or OS versions.

      import datetime
      “”"requires:”"”
      d = datetime.datetime.now()

      as opposed to:

      from datetime import datetime
      “”"requires:”"”
      d = datetime.now()

  12. [...] This post was mentioned on Twitter by Pedro Assuncão, mdevraj. mdevraj said: Get started with WebSockets, http://diffract.me/2009/12/websockets-tutorialexample-with-pywebsocket/ [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>