Search code examples
pythontwistedautobahnwamp-protocol

Can autobahn.twisted.wamp.Application do pub/sub?


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.


Solution

  • 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.