Scille / autobahn-sync

Bring autobahn to your synchronous apps !
MIT License
17 stars 3 forks source link

README example works for call() but not publish()/subscribe() #3

Open rbcollins123 opened 7 years ago

rbcollins123 commented 7 years ago

We were going to leverage this for a project, so I popped the example in to quickly evaluate it, but it didn't seem to run as expected out of the box?

When I ran the example in a fresh virtualenv for python 2.7 and 3.5, i see no output from the publish() calls. I flipped the print statements over to a logger and still saw no output?

In [1]: from time import sleep
   ...: from autobahn_sync import publish, call, register, subscribe, run
   ...:
   ...: import logging
   ...: logging.basicConfig(level=logging.DEBUG)
   ...: logger = logging.getLogger(__name__)
   ...:
   ...: @register('com.app.shout')
   ...: def shout(msg):
   ...:     return msg.upper()
   ...:
   ...:
   ...: @subscribe('com.app.idea')
   ...: def on_thought(msg):
   ...:     logger.info("I've just had a new idea: %s" % msg)
   ...:
   ...:
   ...: run()
   ...: while True:
   ...:     # print(call('com.app.shout', 'Autobahn is cool !'))
   ...:     publish('com.app.idea', 'Use autobahn everywhere !')
   ...:     sleep(1)
   ...:
INFO:twisted:Log opened.
DEBUG:autobahn:[MainThread] call bootstrap
DEBUG:autobahn:[CrochetReactor] start bootstrap
DEBUG:autobahn:[CrochetReactor] end bootstrap
DEBUG:autobahn:[CrochetReactor] start register_session
DEBUG:autobahn:Received WELCOME answer to our HELLO: Welcome(session=6306811870588809, roles={u'broker': broker(publisher_identification=True, pattern_based_subscription=True, subscription_meta_api=True, payload_encryption_cryptobox=True, payload_transparency=True, subscriber_blackwhite_listing=True, session_meta_api=True, publisher_exclusion=True, subscription_revocation=True), u'dealer': dealer(payload_encryption_cryptobox=True, payload_transparency=True, pattern_based_registration=True, registration_meta_api=True, shared_registration=True, caller_identification=True, session_meta_api=True, registration_revocation=True, progressive_call_results=True)}, realm=realm1, authid=53E5-HEUF-F9XR-QC3E-MMTP-WFVW, authrole=anonymous, authmethod=anonymous, authprovider=static, authextra=None)
DEBUG:autobahn:[CrochetReactor] callback resolve: Welcome(session=6306811870588809, roles={u'broker': broker(publisher_identification=True, pattern_based_subscription=True, subscription_meta_api=True, payload_encryption_cryptobox=True, payload_transparency=True, subscriber_blackwhite_listing=True, session_meta_api=True, publisher_exclusion=True, subscription_revocation=True), u'dealer': dealer(payload_encryption_cryptobox=True, payload_transparency=True, pattern_based_registration=True, registration_meta_api=True, shared_registration=True, caller_identification=True, session_meta_api=True, registration_revocation=True, progressive_call_results=True)}, realm=realm1, authid=53E5-HEUF-F9XR-QC3E-MMTP-WFVW, authrole=anonymous, authmethod=anonymous, authprovider=static, authextra=None)
DEBUG:autobahn:[MainThread] call decorated register/subscribe
DEBUG:autobahn:[MainThread] start callbacks runner
rbcollins123 commented 7 years ago

So, while not obvious to a 1st time user of Autobahn, the example does work as it should. Default behavior of publish() is to exclude delivery of the message back to the endpoint that initially published the message (unless explicitly allowed via setting exclude_me=False) and therefor the subscription method never fires.

I would suggest modifying the README to make it run in a more "expected" manner for 1st time users, since I would think lots of users of autobahn-sync may be looking for a way to quickly connect to autobahn/crossbar infrastructure without full knowledge of the async Autobahn's behavior.

Below were my edits to the introductory example in case you want to update the README or in case it helps others during their initial investigation:

from time import sleep
from autobahn_sync import publish, call, register, subscribe, run
from autobahn.wamp.types import PublishOptions 

@register('com.app.shout')
def shout(msg):
    return msg.upper()

@subscribe('com.app.idea')
def on_thought(msg):
    print("I've just had a new idea: %s" % msg)

run()
while True:
    print(call('com.app.shout', 'Autobahn is cool !'))
    publish('com.app.idea', 'Use autobahn everywhere !', options=PublishOptions(exclude_me=False))
    sleep(1)

which results in the the output one would expect to STDOUT:

AUTOBAHN IS COOL !
I've just had a new idea: Use autobahn everywhere !
AUTOBAHN IS COOL !
I've just had a new idea: Use autobahn everywhere !