aboutsummaryrefslogtreecommitdiffstats
path: root/python/examples
diff options
context:
space:
mode:
Diffstat (limited to 'python/examples')
-rw-r--r--python/examples/here-now.py (renamed from python/examples/here-now-example.py)16
-rwxr-xr-xpython/examples/history-example.py12
-rw-r--r--python/examples/history.py34
-rwxr-xr-xpython/examples/publish-example.py44
-rw-r--r--python/examples/publish.py37
-rwxr-xr-xpython/examples/subscribe-example.py70
-rw-r--r--python/examples/subscribe.py46
7 files changed, 127 insertions, 132 deletions
diff --git a/python/examples/here-now-example.py b/python/examples/here-now.py
index 0bc3015..f573389 100644
--- a/python/examples/here-now-example.py
+++ b/python/examples/here-now.py
@@ -20,13 +20,17 @@ ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False
## -----------------------------------------------------------------------
pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,
secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
-crazy = 'hello_world'
+channel = 'hello_world'
-def print_cb(message):
+
+# Synchronous usage
+
+print pubnub.here_now(channel)
+
+# Asynchronous usage
+
+def callback(message):
print(message)
-pubnub.here_now({
- 'channel': crazy,
- 'callback': print_cb
-})
+pubnub.here_now(channel, callback=callback, error=callback)
diff --git a/python/examples/history-example.py b/python/examples/history-example.py
deleted file mode 100755
index 793c612..0000000
--- a/python/examples/history-example.py
+++ /dev/null
@@ -1,12 +0,0 @@
-import sys
-from Pubnub import PubnubAsync as Pubnub
-
-## Initiat Class
-pubnub = Pubnub('demo', 'demo', None, False)
-
-## History Example
-history = pubnub.history({
- 'channel': 'hello_world',
- 'limit': 1
-})
-print(history)
diff --git a/python/examples/history.py b/python/examples/history.py
new file mode 100644
index 0000000..3d6addf
--- /dev/null
+++ b/python/examples/history.py
@@ -0,0 +1,34 @@
+## www.pubnub.com - PubNub Real-time push service in the cloud.
+# coding=utf8
+
+## PubNub Real-time Push APIs and Notifications Framework
+## Copyright (c) 2010 Stephen Blum
+## http://www.pubnub.com/
+
+
+import sys
+from Pubnub import PubnubAsync as Pubnub
+
+publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
+subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
+secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
+cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''
+ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False
+
+## -----------------------------------------------------------------------
+## Initiate Pubnub State
+## -----------------------------------------------------------------------
+pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,
+ secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
+channel = 'a'
+
+# Synchronous usage
+
+print pubnub.history(channel, count=2)
+
+# Asynchronous usage
+
+def callback(message):
+ print(message)
+
+pubnub.history(channel, count=2 , callback=callback, error=callback)
diff --git a/python/examples/publish-example.py b/python/examples/publish-example.py
deleted file mode 100755
index 78b7f36..0000000
--- a/python/examples/publish-example.py
+++ /dev/null
@@ -1,44 +0,0 @@
-## www.pubnub.com - PubNub Real-time push service in the cloud.
-# coding=utf8
-
-## PubNub Real-time Push APIs and Notifications Framework
-## Copyright (c) 2010 Stephen Blum
-## http://www.pubnub.com/
-
-import sys
-from Pubnub import PubnubAsync as Pubnub
-
-publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
-subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
-secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
-cipher_key = len(
- sys.argv) > 4 and sys.argv[4] or '' # (Cipher key is Optional)
-auth_key = len(
- sys.argv) > 5 and sys.argv[4] or 'abcd' # (Cipher key is Optional)
-ssl_on = len(sys.argv) > 6 and bool(sys.argv[5]) or False
-
-## -----------------------------------------------------------------------
-## Initiate Pubnub State
-## -----------------------------------------------------------------------
-pubnub = Pubnub(
- publish_key, subscribe_key, secret_key, cipher_key, auth_key, ssl_on, pooling=True)
-crazy = 'hello_world'
-
-## -----------------------------------------------------------------------
-## Publish Example
-## -----------------------------------------------------------------------
-
-
-def publish_complete(info):
- print(info)
-
-
-def publish_error(info):
- print('ERROR : ' + str(info))
-
-import time
-start = time.time()
-for i in range(1,100):
- print pubnub.publish(crazy, 'hello world-' + str(i))
-end = time.time()
-print(end - start);
diff --git a/python/examples/publish.py b/python/examples/publish.py
new file mode 100644
index 0000000..029c17a
--- /dev/null
+++ b/python/examples/publish.py
@@ -0,0 +1,37 @@
+## www.pubnub.com - PubNub Real-time push service in the cloud.
+# coding=utf8
+
+## PubNub Real-time Push APIs and Notifications Framework
+## Copyright (c) 2010 Stephen Blum
+## http://www.pubnub.com/
+
+
+import sys
+from Pubnub import PubnubAsync as Pubnub
+
+publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
+subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
+secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
+cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''
+ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False
+
+## -----------------------------------------------------------------------
+## Initiate Pubnub State
+## -----------------------------------------------------------------------
+pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,
+ secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
+channel = 'hello_world'
+message = 'Hello World !!!'
+
+
+
+# Synchronous usage
+
+print pubnub.publish(channel, message)
+
+# Asynchronous usage
+
+def callback(message):
+ print(message)
+
+pubnub.publish(channel, message, callback=callback, error=callback)
diff --git a/python/examples/subscribe-example.py b/python/examples/subscribe-example.py
deleted file mode 100755
index 9c16bbe..0000000
--- a/python/examples/subscribe-example.py
+++ /dev/null
@@ -1,70 +0,0 @@
-import sys
-import threading
-import time
-import random
-import string
-from Pubnub import PubnubAsync as 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()
diff --git a/python/examples/subscribe.py b/python/examples/subscribe.py
new file mode 100644
index 0000000..a410297
--- /dev/null
+++ b/python/examples/subscribe.py
@@ -0,0 +1,46 @@
+## www.pubnub.com - PubNub Real-time push service in the cloud.
+# coding=utf8
+
+## PubNub Real-time Push APIs and Notifications Framework
+## Copyright (c) 2010 Stephen Blum
+## http://www.pubnub.com/
+
+
+import sys
+from Pubnub import PubnubAsync as Pubnub
+
+publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
+subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
+secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo'
+cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''
+ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False
+
+## -----------------------------------------------------------------------
+## Initiate Pubnub State
+## -----------------------------------------------------------------------
+pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,
+ secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
+
+channel = 'a'
+
+
+
+# Asynchronous usage
+
+def callback(message, channel):
+ print(message)
+
+def error(message):
+ print("ERROR : " + str(message))
+
+def connect(message):
+ print("CONNECTED")
+
+def reconnect(message):
+ print("RECONNECTED")
+
+def disconnect(message):
+ print("DISCONNECTED")
+
+
+pubnub.subscribe(channel, callback=callback, error=callback, connect=connect, reconnect=reconnect, disconnect=disconnect)