diff options
| author | Devendra | 2013-02-24 00:58:24 +0530 |
|---|---|---|
| committer | Devendra | 2013-02-24 00:58:24 +0530 |
| commit | 7d76f9d0d9c04c2fbc7740a08122fdc4c5622fa3 (patch) | |
| tree | 4c71abce3b3c8d0fa7ab217f5b742aa2fbd36e86 /python/3.2/subscribe-example.py | |
| download | pubnub-python-7d76f9d0d9c04c2fbc7740a08122fdc4c5622fa3.tar.bz2 | |
copying python, python-twisted, python-tornado
Diffstat (limited to 'python/3.2/subscribe-example.py')
| -rwxr-xr-x | python/3.2/subscribe-example.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/python/3.2/subscribe-example.py b/python/3.2/subscribe-example.py new file mode 100755 index 0000000..e458e2b --- /dev/null +++ b/python/3.2/subscribe-example.py @@ -0,0 +1,64 @@ +import sys +import threading +import time +import random +import string +from Pubnub import Pubnub + +## Initiate Class +pubnub = Pubnub( 'demo', 'demo', None, False ) + +print("My UUID is: "+pubnub.uuid) + +channel = ''.join(random.choice(string.ascii_letters + string.digits) for x in range(20)) + +## Subscribe Example +def receive(message) : + print(message) + return False + +def pres_event(message): + print(message) + return False + +def subscribe(): + print("Listening for messages on '%s' channel..." % channel) + pubnub.subscribe({ + 'channel' : channel, + 'callback' : receive + }) + +def presence(): + print("Listening for presence events on '%s' channel..." % channel) + pubnub.presence({ + 'channel' : channel, + 'callback' : pres_event + }) + +def publish(): + print("Publishing a test message on '%s' channel..." % channel) + pubnub.publish({ + 'channel' : channel, + 'message' : { 'text':'foo bar' } + }) + +pres_thread = threading.Thread(target=presence) +pres_thread.daemon=True +pres_thread.start() + +sub_thread = threading.Thread(target=subscribe) +sub_thread.daemon=True +sub_thread.start() + +time.sleep(3) + +publish() + + +print("waiting for subscribes and presence") +pres_thread.join() + +print pubnub.here_now({'channel':channel}) + +sub_thread.join() + |
