[Twisted-web] RE: [Twisted-Python] Chat by html application

Harald.Nyman at astrazeneca.com Harald.Nyman at astrazeneca.com
Fri May 27 10:45:23 MDT 2005

Hi Tibi,

Nevow is probably the best. But there are other ways too. I've been
doing this for several years with a Tomcat servlet, and then there was
no Nevow. After converting to Pythonism, I switched to a Twisted server.
Haven't learned Nevow yet, despite good intentions..., but I think this
may be similar. It's essentially doing all of "What I want to do:"
below, only I'm not using it mainly for chat, but for distributing
HTML or Javascript, for viewing things together during a phone meeting.

 - the browser's background HTTPRequest sits in a threaded applet
 - someone switches on "collaboration" in his browser - the others see
   his name being added to the "virtual meeting"
 - one user drags a pointer (image) in his browser -> the pointer moves
   in the other browsers

See also some A's to your Q's, below.

On 5/26/05, Tibi Dondera <incoming at pronet-romania.com> wrote:
> Hello all,
> I'm trying to develop a chat application through a browser, using
> twisted (if possible)
> What I want to do:
>  - a user logs in the website
>  - he goes to the "chat page"
>  - he has the "classic" chat interface (sent messages, list of online
> users, "send a message" field). All of these are simple "divs"
>  - when he sends a message, a post with the message is submitted to the
> server, and registered in a temporary database table (or in memory, is
> there some "temporary" memory that I can use in twisted for this purpose
> ?)
>  - for refreshing the chat area, there is an open HTTPRequest in the
> background. Twisted will do the following:
>      - accept the HTTPrequest connection
>      - keep it alive (i.e. do not send the answer, but maybe send some
> empty chars to force the browser to keep the connection open)
>      - when a new message (from the other users) is available, send it
> to the user and close the connection.
>  - in the user's browser, when the connection is closed (either with a
> message, or after a certain timeout, that cannot be avoided anyway), a
> new one will be opened, until the user quits the chat.
> Questions:
> - Is this a plausible solution ?

Yes, IMHO.

> - Has this already been implemented (or at least partly) ?
> - Can this actually be done with twisted ?


> - Can I develop this on Windows ? (production will be on linux, but
> still, I prefer developing local, and currently on windows)

Hmm.. I develop "on Windows", but with a VMWare Linux server on the
same laptop. Works great, but costs a bit. Should work with the server
on Windows, too(?)

> - Can you give me any pointers to some existing code, or where to start
> ? (maybe jabber for twisted ?)

Here's a few fragments from my server code -

class Servlet(resource.Resource):
# Send queued output to browser's background listener
    def render_GET(self, request):
        self.request = request
        session = request.getSession()
        anything = 0
        while not session.outqueue.empty():
            x = session.outqueue.get()
            anything = 1
        if anything:
        return server.NOT_DONE_YET
    def render_POST(self, request):  
        session = request.getSession()
# Browser sends a dictionary as text
        msgDict = eval(request.args['message'][0])
        message = msgDict['message']
        mSplit = message.split()
        ... # Do work here
        return server.NOT_DONE_YET

> - How will a twisted http server coexist with my existing apache server
> on the 80 port ?

Not well, I guess. I use a different port for the Twisted server.

> - Must I subscribe to the twisted-http mailing list instead and ask this
> question there ?

Twisted-web would be better, yes. I'm posting this one there too, with all
due apologies...

> Thanks for any help.
> Tibi Dondera

Twisted-Python mailing list
Twisted-Python at twistedmatrix.com

More information about the Twisted-web mailing list