Single AppSession cannot subscribe and publish on the same thread
Based on a simple Hello World example, I'll replace the topic oncounter
with onhello
when posting. That would mean AppSession
subscribing to a topic that she herself publishes. I am assuming that it should be able to receive its own messages, but it looks like it is not. Is there a way to do this?
For a reproducible example:
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.util import sleep
from autobahn.twisted.wamp import ApplicationSession
class AppSession(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
def onhello(msg):
print("event for 'onhello' received: {}".format(msg))
sub = yield self.subscribe(onhello, 'com.example.onhello')
counter = 0
while True:
yield self.publish('com.example.onhello', counter)
print("published to 'onhello' with counter {}".format(counter))
counter += 1
yield sleep(1)
code>
After launching crossbar start
, I see the published text onhello
, but it is not received.
source to share
This is because - by default - the publisher does not receive the published event, even if the publisher has subscribed to the published topic itself.
You can change this behavior by providing an argument options
for publish()
:
yield self.publish('com.example.onhello', counter,
options = autobahn.wamp.types.PublishOptions(excludeMe = False))
source to share