Reconnecting to Crossbar using autobahn.twisted.wamp.Application

#1

Hi.

I am trying to create and application that makes WAMP RPC Calls via AutoBahn Python from within a Flask Application.

The Tavendo team has created some great examples on how to do exactly that. The one I am following is using Crochet to wrap the asynchronous WAMP calls so that they can be called from a Flask web request handler:

https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/wamp/app/crochet/example2/server.py

It works great, except for the fact that periodically I lose connection to Crossbar and have to restart the whole application.

The various examples I have found for ReconnectingClientFactory all expect the use of WampWebSocketClientFactory and a Component to handle the WAMP calls, but that does not seem to work in this situation since I need to have one persistent WAMP Session that is called from outside of of the application component (from the Flask Handler)

https://gist.github.com/DenJohX/e6d0864738da10cb9685

https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/websocket/reconnecting/client.py

Can anybody help shed some light on how to allow reconnection in this application?

from flask import Flask, request

from crochet import setup, run_in_reactor, wait_for

Crochet says this MUST be called before any Autobahn or Twisted imports!

setup()

from twisted.internet.defer import returnValue # noqa

from autobahn.twisted.util import sleep # noqa

from autobahn.twisted.wamp import Application # noqa

WAMP app

wapp = Application()

Sample RPC Call - Normall Registered by something else…

@wapp.register(‘some.example’)

def do_something(x):

print(“RPC Call do_something() called with {}”.format(x))

return x

synchronous wrappers around WAMP RPC Call

@wait_for(timeout=5)

def call_something(x):

return wapp.session.call(‘some.example’, x)

our Flask app

app = Flask(name)

@app.route(’/something’, methods=[‘GET’])

def something():

res = call_something(“Hello World”)

return “RPC Call call_something returned {}”.format(res)

if name == ‘main’:

import sys

import logging

logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

Starting WAMP app on a background thread and setup communication

with the main thread that runs a (blocking) Flask server

@run_in_reactor

def start_wamp():

wapp.run(“wss://demo.crossbar.io/ws”, “realm1”, start_reactor=False)

start_wamp()

starting the Flask Server

app.run(port=8080)

``

Thanks!

Robin

0 Likes

#2

Bumping this thread in the hopes that someone has made some progress on automatically reconnecting from Autobahn Python. Not supporting auto reconnection from the Application object makes it very difficult to leverage WAMP from python in other frameworks.

Any ideas?

There seems to be an issue for it: https://github.com/tavendo/AutobahnPython/issues/295, but no activity for a couple months…

Thanks.

Robin

0 Likes

#3

Hi Robin!

First of all sorry for the late answer.

Automatic reconnecting is underway, and the two issues relating to it (https://github.com/tavendo/AutobahnPython/issues/389 and https://github.com/tavendo/AutobahnPython/issues/295) have now been assigned to the 10.7 release milestone. With 10.6 just released, I hope you won’t have to wait too much longer.

Regards,

Alex

···

Am Montag, 24. August 2015 23:40:49 UTC+2 schrieb Robin Cavanaugh:

Bumping this thread in the hopes that someone has made some progress on automatically reconnecting from Autobahn Python. Not supporting auto reconnection from the Application object makes it very difficult to leverage WAMP from python in other frameworks.

Any ideas?

There seems to be an issue for it: https://github.com/tavendo/AutobahnPython/issues/295, but no activity for a couple months…

Thanks.

Robin

0 Likes