pythonwamptwistedautobahncrossbar

Connection to crossbar.io either hangs or disconnects


I'm trying to connect to crossbar on a remote host using Python with autobahn[Twisted]

I'm using a modified example code from PubSub:

from __future__ import print_function
from os import environ
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner

class Component(ApplicationSession):
    def __init__(self, config=None):
       ApplicationSession.__init__(self, config)
        print("component created")

   def onConnect(self):
        print("transport connected")
        self.join(self.config.realm)

    def onChallenge(self, challenge):
        print("authentication challenge received")

    @inlineCallbacks
    def onJoin(self, details=None):
        print("session attached")
        self.received = 0
        for x in range(1, 501):
            sub = yield self.subscribe(self.on_event, u'com.myapp.topic{}'.format(x))
            if x % 100 == 0:
                print("Subscribed to {} topics".format(x))

    def on_event(self, i=None):
        print("Got event: {}".format(i))
        self.received += 1
        self.config.extra for configuration, etc. (see [A])
        if self.received > self.config.extra['max_events']:
            print("Received enough events; disconnecting.")
            self.leave()

    def onDisconnect(self):
        print("disconnected")
        if reactor.running:
            reactor.stop()


if __name__ == '__main__':
    runner = ApplicationRunner(
        url=u"ws://localhost:8080/ws",
        realm=u"realm1",
        extra=dict(
            max_events=5000,  # [A] pass in additional configuration
        ),
    )
    print(runner.log)
    runner.run(Component)

I have an instance of crossbar running on my localhost for testing, and when I access that, everything works.

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected
2016-04-01T17:26:16+0000 session attached
(stuff happens here, events get published, until max is reached)
2016-04-01T17:26:19+0000 Received SIGINT, shutting down.
2016-04-01T17:26:19+0000 disconnected
2016-04-01T17:26:19+0000 Main loop terminated.

But if I try to connect to other hosts, two things will happen: If it's a secure port:

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected

(the session never gets attached, program hangs)

If it's an insecure port:

2016-04-01T17:26:16+0000 component created
2016-04-01T17:26:16+0000 transport connected
2016-04-01T17:26:19+0000 disconnected
2016-04-01T17:26:19+0000 Main loop terminated.

(the connection kicks me out automatically before the session gets attached

The host I'm trying to connect to has 8080 for secure port and 8081 for insecure. So all I change is:

url=u'ws://{hostname}:8080/ws', (or)
url=u'ws://{hostname}:8081/ws',

I wanna know if I'm missing something obvious about WAMP connections or if this is probably a configuration issue on the crossbar instance I'm trying to connect to.


Solution

  • I fixed my problem by running this on a virtualenv with the following versions of autobahn and twisted:

    autobahn==0.10.2
    Twisted==15.1.0
    

    I don't know why the latest versions were behaving this way, I just know that my code above will only work on those versions.