Skip to main content

txWS Upgrade

txWS-Upgrade (pronounced “Twisted WebSockets Upgrade”) is a small, short, simple library for adding WebSockets server support to your favorite Twisted applications.

This is forked from txWS to add upgrade support. https://github.com/MostAwesomeDude/txWS

Usage

Use txwebsocket.txws.WebSocketFactory to wrap your factories. That’s it! Adding WebSockets support has never been easier.

>>> from txwebsocket.txws import WebSocketFactory
>>> reactor.listenTCP(8080, WebSocketFactory(factory_to_wrap))

There is no extra trick to txWS. There is no special setup involved.

Do you want secure WebSockets? Use listenSSL() instead of listenTCP().

Upgrade Usage

If you want to use the websocket with in an existing site, Update your code as follows.

This is a vanilla Twisted website.

from twisted.web import server
from twisted.web.resource import Resource
from twisted.internet import reactor, endpoints

class Simple(Resource):
    isLeaf = True
    def getChild(self, name, request):
        if name == '':
            return self
        return Resource.getChild(self, name, request)

    def render_GET(self, request):
        return "Hello, world! I am located at %r." % (request.prepath,)

rootResource = Simple()
site = server.Site(rootResource)

endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
endpoint.listen(site)
reactor.run()
Now add the website has support for WebSockets, to include

the txwebsocket.txws.WebSocketUpgradeResource and txwebsocket.txws.WebSocketUpgradeHTTPChannel.

from twisted.web import server
from twisted.web.resource import Resource
from twisted.internet import reactor, endpoints

class Simple(Resource):
    isLeaf = True
    def getChild(self, name, request):
        if name == '':
            return self
        return Resource.getChild(self, name, request)

    def render_GET(self, request):
        return "Hello, world! I am located at %r." % (request.prepath,)

rootResource = Simple()
site = server.Site(rootResource)

# 1) Add the imports
#    Create the WebSocketFactory
#    Create the WebSocketUpgradeResource
#    Put the resource into the resource tree
from txwebsocket.txws import WebSocketFactory, WebSocketUpgradeResource
rootResource.putChild(b"websocket",
                      WebSocketUpgradeResource(WebSocketFactory(factory_to_wrap)))


# 2) Add the imports
#    Replace protocol for the website with the Websocket upgradable ones
from txwebsocket.txws import WebSocketUpgradeHTTPChannel
site.protocol = VortexWebsocketHTTPChannel

endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
endpoint.listen(site)
reactor.run()

Versions

txWS supports the following versions of the WebSockets draft:

  • Version 76

    • Hixie-76 (Chrome 6, Fx 4, Opera 11, UNTESTED Safari 5)

    • HyBi-00

  • Version 7

    • HyBi-07 (Fx 6)

  • Version 8

    • HyBi-08

    • HyBi-10 (Chrome 14, Chrome 15, Fx 7, Fx 8)

  • Version 13

    • RFC 6455 (Chrome 16)

All listed browser versions have been tested and verified working; any browser marked “UNTESTED” hasn’t been personally tested, but has been reported working by third parties.

In case you’re wondering, the version numbers above are correct; WebSockets versioning is not sane.

Browser Quirks

This might save you some time when developing your WebSockets-based application.

  • Firefox (all versions): WebSockets do not follow the standard WebSocket API.

  • Opera 11: WebSockets are disabled by default and are very slow to close connections.

Comparisons

Here’s how txWS compares to other Twisted WebSockets libraries.

txWebSockets

txWS, unlike txWebSockets, doesn’t reuse any HTTP machinery and doesn’t pretend to be HTTP. Whether this is a good or bad thing depends largely on whether the WebSockets standard ends up being a valid HTTP subset.

txWS supports newer WS versions 7 and 8, but txWebSockets supports the older version 75. Both libraries support version 76.

Autobahn

Autobahn provides a client library for WebSockets as well as a server, and provides a fancy set of messaging protocols on top of the WS layer. Autobahn also provides support for WS version 10.

However, Autobahn doesn’t provide support for WS version 76, and requires clients to subclass their factories and protocols in order to provide WS functionality. txWS uses a compositional approach with wrapped protocols, allowing completely transparent reuse of existing protocols and factories.

Cyclone

Cyclone provides a simple WebSockets handler. This handler can do WS versions 75 and 76. The Cyclone WebSockets handler is very limited, can only wrap other Cyclone handlers, and doesn’t support any of the more modern WebSockets versions.

License

txWS is (c) 2011 Oregon State University Open Source Lab, (c) 2014 Google Inc., and is made available under the Apache 2.0 license.

Thanks

Thank you to all of the contributors in the community who have chipped in to help keep txWS alive.

Metadata

Release files for txwebsocket 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for txwebsocket 1.1.2
File Size Uploaded
txwebsocket-1.1.2.tar.gz 16.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for txwebsocket 1.1.2
File Interpreter ABI Platform
txwebsocket-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 27.8 kB

Release files / txwebsocket-1.1.2.tar.gz

Download URL txwebsocket-1.1.2.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0db3259fdaf528d2bfed3a984690c568556fcb37c42a55abef3eaad85c436908
BLAKE2b-256 checksum
How to use checksums
29f27e08e9efe76c02d7dd41dd17110c337aca08e6cf92863a2796cab9b07178
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / txwebsocket-1.1.2-py3-none-any.whl

Download URL txwebsocket-1.1.2-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0729b8b0088d9033c3c5bbe4f9a7497ac6d8c51512d01d04518aefbe77716ae6
BLAKE2b-256 checksum
How to use checksums
42c982c78f2e0d58092a8ede1789279404266cd0741e9a2938449be1744c5e0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

1 release file

1.1.0

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page