12

In my ongoing curiosity about websockets, I'm noticing a trend:

The "hello world" of the websocket universe, at least at the moment, seems to be "echo" functionality. That is, the demonstrated application is typically, "I send something, I receive something."

While aptly demonstrating that the protocol is functional, this example only actually demonstrates the same type of communication that the traditional request / response cycle enables.

For example, the only demonstration (on the server side) that I can find of twisted.web.websockets is the following:

import sys
from twisted.python import log
from twisted.internet import reactor
from twisted.web.static import File
from twisted.web.websocket import WebSocketHandler, WebSocketSite


class Echohandler(WebSocketHandler):

    def frameReceived(self, frame):
        log.msg("Received frame '%s'" % frame)
        self.transport.write(frame + "\n")


def main():
    log.startLogging(sys.stdout)
    root = File(".")
    site = WebSocketSite(root)
    site.addHandler("/ws/echo", Echohandler)
    reactor.listenTCP(8080, site)
    reactor.run()


if __name__ == "__main__":
    main()

How can I instead examine "push" capability here? ie, how I can leave the web socket open, and then later, at some time determined by the occurrence of some event, send a message through the websocket, the content of which is also influenced by this event?

(Those interested by this question might also regard as compelling this question that I asked a few days ago: Making moves w/ websockets and python / django ( / twisted? ))

Community
  • 1
  • 1
jMyles
  • 11,772
  • 6
  • 42
  • 56
  • Given that websockets are disabled in Firefox 4 http://hacks.mozilla.org/2010/12/websockets-disabled-in-firefox-4/ , and even when they're done, they might want not work in all browsers, you probably want to investigate a higher-level abstraction for your two-way browser communication. Websockets are just a way to optimize things like Athena http://divmod.org/trac/wiki/DivmodNevow/Athena and Orbited http://orbited.org/ . – Glyph Dec 10 '10 at 16:51
  • As shoddy as the current spec (and support) is, aren't websockets actually a complete remake of Comet (Athena, Orbited, etc.?). For the moment, I'm not interested in solutions that will work in all browsers; I just want to build something that will work between myself and other members of a collective - we'll have full control over our browser and firewall conditions. – jMyles Dec 10 '10 at 17:09
  • 1
    No, not necessarily. Athena is a complete high-level messaging system, it doesn't just transport bytes. And Orbited can use multiple transports, falling back to more primitive / less efficient techniques as it determines that the better ones aren't available. This is something you want even in your situation, because if something *better* than websockets comes along, you'd probably like to take advantage of it without having to rewrite all your code. – Glyph Dec 10 '10 at 17:13
  • 1
    @Glyph: Amazing. This is point #6 in the question I asked here: http://stackoverflow.com/questions/4310706/django-comet-push-least-of-all-evils (outstanding 50 point bounty! ;-)). Can you show a quick demonstration of how Orbited can be used with Websockets and then fall back to long-polling in the absence of proper support? – jMyles Dec 10 '10 at 17:28

2 Answers2

9

This is an example of an updated EchoHandler that will instead of just being reactive, be proactive.

class ChattyHandler(WebSocketHandler):
    def connectionMade(self):
        self.transport.write('oh hai\n')
        self.saysomething()

    def saysomething(self):
        self.transport.write('still there?\n')
        reactor.callLater(5, self.saysomething)

Unfortunately, websockets from https://github.com/rlotun/txWebSocket/ doesn't seem to have the connectionMade() method, and instead the only thing you can hook into is the __init__. usually you would just override connectionMade() if it were a 'normal' twisted protocol. --Fixed in upstream

Jerub
  • 41,746
  • 15
  • 73
  • 90
  • 1
    This raises TypeError: __init__() argument after * must be a sequence, not WebSocketTransport – jMyles Dec 10 '10 at 06:57
  • 4
    I maintain https://github.com/rlotun/txWebSocket/ and I have just added a `connectionMade` hook. – rlotun Dec 23 '10 at 10:44
0

Using hendrix, I showed how to set up a web app in a talk at Django-NYC that uses websockets to push messages from a telnet server to a web page.

jMyles
  • 11,772
  • 6
  • 42
  • 56