1

I would like to use some pub/sub features along with rpc from autobahn.twisted.wamp.Application

I'd prefer not to make a ApplicationSession class if I can get by without doing so.

Can registered rpc methods cause client subscriptions and publish? If they can, please show me how.

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
John
  • 13
  • 2

1 Answers1

4

Yes, sure:

def onEvent(msg):
   print("got event: {}".format(msg))

@app.register('com.example.triggersubscribe')
def triggerSubscribe():
   yield app.session.subscribe(onEvent, 'com.example.topic1')

When triggerSubscribe is called (e.g. remotely from another WAMP component), the callee (the WAMP component exposing com.example.triggersubscribe) will dynamically subscribe to com.example.topic1.

You can publish from within a registered procedure also of course: app.session.publish().

I have added the complete example (including JS client) here.

oberstet
  • 21,353
  • 10
  • 64
  • 97
  • How can you do this and have `triggerSubscribe` return a value back to the caller? Python forbids a `return` statement in a generator. – Jon Nov 15 '14 at 22:14
  • 1
    In Twisted/Python2, use `returnValue` (http://twistedmatrix.com/documents/current/api/twisted.internet.defer.html#returnValue). There are analog mechnisms for Trollius/Python2 and asyncio/Python3. – oberstet Nov 15 '14 at 22:27