0

I'm trying to integrate a RESTful responder in a Crossbar application, for which the best fit seems to be a WSGI service. This service ideally should be part of the rest of the pub/sub infrastructure, being able to receive WAMP events on the one hand and answer HTTP requests on the other.

The difficulty is to run an event loop which allows asynchronous web socket events and additionally offer a WSGI compliant component. It seems to me that Pulsar should be able to do that, but I have not been able to figure out how to set it up, none of the available samples demonstrate exactly this use case.

value = None

class Foo(ApplicationSession):
    def onJoin(self, details):
        yield self.subscribe(self.bar, 'bar')

    def bar(self, data):
        value = data


app = Flask(__name__)

@app.route('/')
def baz():
    return value


if __name__ == '__main__':
    runner = ApplicationRunner('ws://127.0.0.1:8080', 'test')
    runner.run(Foo, start_reactor=False)

    # now what?

The above demonstrates the two parts, an Autobahn WAMP client and a Flask WSGI component. How do I run both of these in parallel, allowing one thread to receive events both via HTTP and web socket? I don't particularly care about the version of Python nor underlying library (Twisted, asyncio, Pulsar, Flask), I'd just like to get this running somehow.

deceze
  • 510,633
  • 85
  • 743
  • 889

1 Answers1

1

WSGI is an inherently synchronous API. I don't know about Pulsar, but I would be surprised if it could somehow magically work around this fact.

The way Crossbar.io integrates with classic Web (and synchronous) stacks is via a REST-bridge. Currently, we have the WAMP "Publisher" role covered today (2015/02): that is, you can publish an WAMP event by doing a simple HTTP/POST http://crossbar.io/docs/HTTP-Pusher-Service/. This REST bridge in Crossbar.io will be extended to cover all 4 WAMP roles in the near future.

If you take a step back, and primarily care about something do create a REST API in your app, and which integrates directly with WAMP and asynchronous stuff, I'd have a look a Twisted Klein. Twisted Klein is essentially modeled after Flask, but at the source level. We have a blog post that covers exactly this: Mixing Web and WAMP code with Twisted Klein

oberstet
  • 21,353
  • 10
  • 64
  • 97
  • I need to expose a REST endpoint to a third party which can only contact me via plain old HTTP. On this endpoint I need to deliver data which is being passed around using WAMP in my Crossbar app. Ideally I'd like to run a component persistently which is subscribed to a certain topic, populates its internal data from messages in this topic, and delivers this data via HTTP when asked for. And all on the same Crossbar server if possible. Otherwise I'll have to set up some external data passing/storage method. Can Klein do that, run as WSGI? – deceze Feb 16 '15 at 12:54
  • Yes, Klein will make that easy (like probably 50 LOC). There is no WSGI with Klein. It just creates a Twisted Web resource that *looks and feels like Flask* (but isn't Flask, and isn't WSGI). To hook that into Crossbar.io https://github.com/crossbario/crossbar/issues/247 – oberstet Feb 16 '15 at 15:36
  • As another side note: when we have the REST bridge done fully, you can call into any WAMP procedure via plain old HTTP/POSTs. Just by configuration of Crossbar.io - not a single line of code. The last event published under some topic however (currently) is not available from a builtin procedure. So you would still need a tiny WAMP component subscribing to the topic, remembering the event, and exposing a single procedure for calling into via HTTP/POST / REST-Bridge. – oberstet Feb 16 '15 at 15:39
  • I'm very much looking forward to this, but I need a solution *now*. :) I'll have a look at Klein, thank you. It does not sound like it's possible to actually hook this into Crossbar *today* though, is it? – deceze Feb 17 '15 at 01:10
  • I've actually had something working on asyncio Tornado + Autobahn already, I've now rewritten this in Twisted + Klein in anticipation of being able to hook it into Crossbar eventually. Until then I'll simply have to run it as a guest on a different port, correct? – deceze Feb 17 '15 at 02:38
  • Exactly. You can run it in a guest worker (under control of Crossbar.io), or you could run it totally independently. And yes: the feature to have it as a Web resource started under Crossbar.io is not here today. It's kinda trivial to implement .. I see what I can do. When it's there, what it buys you, is being able to run your Klein app thing on the same port as the WAMP router. – oberstet Feb 17 '15 at 07:16
  • Thanks a lot! That's good enough for the time being and for the intended purpose; but eventually I'd really like to have a unified server and URL structure. :) – deceze Feb 17 '15 at 07:21