Package: txsockjs
A simple library for adding SockJS support to your twisted application.StatusSockJS-Twisted passes allSockJS-Protocol v0.3.3tests,
and allSockJS-Client qunittests. It has been used in
production environments, and should be free of any critical bugs.UsageUsetxsockjs.factory.SockJSFactoryto wrap your factories. That’s it!fromtwisted.internetimportreactorfromtwisted.internet.protocolimportFactory,Protocolfromtxsockjs.factoryimportSockJSFactoryclassHelloProtocol(Protocol):defconnectionMade(self):self.transport.write('hello')self.transport.write('how are you?')defdataReceived(self,data):printdatareactor.listenTCP(8080,SockJSFactory(Factory.forProtocol(HelloProtocol)))reactor.run()There is nothing else to it, no special setup involved.Do you want a secure connection? UselistenSSL()instead oflistenTCP().Advanced UsageFor those who want to host multiple SockJS services off of one port,txsockjs.factory.SockJSMultiFactoryis designed to handle routing for you.fromtwisted.internetimportreactorfromtwisted.internet.protocolimportFactory,Protocolfromtxsockjs.factoryimportSockJSMultiFactoryfromtxsockjs.utilsimportbroadcastclassEchoProtocol(Protocol):defdataReceived(self,data):self.transport.write(data)classChatProtocol(Protocol):defconnectionMade(self):ifnothasattr(self.factory,"transports"):self.factory.transports=set()self.factory.transports.add(self.transport)defdataReceived(self,data):broadcast(data,self.factory.transports)defconnectionLost(self,reason):self.factory.transports.remove(self.transport)f=SockJSMultiFactory()f.addFactory(Factory.forProtocol(EchoProtocol),"echo")f.addFactory(Factory.forProtocol(ChatProtocol),"chat")reactor.listenTCP(8080,f)reactor.run()http://localhost:8080/echoandhttp://localhost:8080/chatwill give you access
to your EchoFactory and ChatFactory.Integration With WebsitesIt is possible to offer static resources, dynamic pages, and SockJS endpoints off of
a single port by usingtxsockjs.factory.SockJSResource.fromtwisted.internetimportreactorfromtwisted.internet.protocolimportFactory,Protocolfromtwisted.webimportresource,serverfromtxsockjs.factoryimportSockJSResource# EchoProtocol and ChatProtocol defined aboveroot=resource.Resource()root.putChild("echo",SockJSResource(Factory.forProtocol(EchoProtocol)))root.putChild("chat",SockJSResource(Factory.forProtocol(ChatProtocol)))site=server.Site(root)reactor.listenTCP(8080,site)reactor.run()Multiplexing [Experimental]SockJS-Twisted also has built-in support for multiplexing. See theWebsocket-Multiplexlibrary
for how to integrate multiplexing client side.fromtwisted.internetimportreactorfromtwisted.internet.protocolimportFactory,Protocolfromtwisted.webimportresource,serverfromtxsockjs.multipleximportSockJSMultiplexResourcemultiplex=SockJSMultiplexResource()multiplex.addFactory("echo",Factory.forProtocol(EchoProtocol))multiplex.addFactory("chat",Factory.forProtocol(ChatProtocol))root=resource.Resource()root.putChild("multiplex",multiplex)site=server.Site(root)reactor.listenTCP(8080,site)reactor.run()Single factory? Multifactory? Resource? Multiplexing? What’s the difference?TypeFactories per portAllows mixing native web contentFactories per connectionSockJSFactorySingleNoSingleSockJSMultiFactoryMultipleNoSingleSockJSResourceMultipleYesSingleSockJSMultiplexResourceMultipleYesMultipleSockJSFactoryis recommended for use in non-web (HTTP) applications to allow
native web connections. For instance, an IRC server. There can only be one factory
listening on a port using this method. The SockJS endpoint uses this internally.SockJSMultiFactoryis recommended for use in non-web (HTTP) applications with
multiple services. This allows multiple factories to listen on a single port.SockJSResourceis recommended for use in HTTP based applications, like webservers.SockJSMultiplexResourceis recommended for pubsub applications, where each connection
needs to talk to multiple factories. Overriding the subscribe method allows for dynamic
factory creation if you don’t know what is needed server-side ahead of time.EndpointsFor integration with pre-existing libraries or programs, it is possible use sockjs
as an endpoint in the formsockjs:tcp\:9090\:interface\=0.0.0.0:encoding=utf8:websocket=false.
You can pass any escaped endpoint to a sockjs endpoint to wrap it with txsockjs, and you can
specify any option for the SockJSFactory by specifying it as a keyword argument.
For more information, read thetwisted documentation on endpoints.fromtwisted.internetimportreactorfromtwisted.internet.protocolimportFactory,Protocolfromtwisted.internet.endpointsimportserverFromString# Note that we don't have to import anything from txsockjs# HelloProtocol defined aboveendpoint=serverFromString(reactor,"sockjs:tcp\:8080")endpoint.listen(Factory.forProtocol(HelloProtocol))reactor.run()OptionsA dictionary of options can be passed into the factory to control SockJS behavior.options={'websocket':True,'cookie_needed':False,'heartbeat':25,'timeout':5,'streaming_limit':128*1024,'encoding':'cp1252',# Latin1'sockjs_url':'https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.js','proxy_header':None}SockJSFactory(factory_to_wrap,options)SockJSMultiFactory().addFactory(factory_to_wrap,prefix,options)SockJSResource(factory_to_wrap,options)SockJSMultiplexResource(options)websocket :whether websockets are supported as a protocol. Useful for proxies or load balancers that don’t support websockets.cookie_needed :whether the JSESSIONID cookie is set. Results in less performant protocols being used, so don’t require them unless your load balancer requires it.heartbeat :how often a heartbeat message is sent to keep the connection open. Do not increase this unless you know what you are doing.timeout :maximum delay between connections before the underlying protocol is disconnectedstreaming_limit :how many bytes can be sent over a streaming protocol before it is cycled. Allows browser-side garbage collection to lower RAM usage.encoding :All messages to and from txsockjs should be valid UTF-8. In the event that a message received by txsockjs is not UTF-8, fall back to this encoding.sockjs_url :The url of the SockJS library to use in iframes. By default this is served over HTTPS and therefore shouldn’t need changing.proxy_header :The HTTP header to pull a proxied IP address out of. Leave as None to get the unproxied IP.Do not change this unless you are behind a proxy you control.LicenseSockJS-Twisted is (c) 2012 Christopher Gamble and is made available under the BSD license.