-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
36 lines (26 loc) · 945 Bytes
/
Copy pathapp.py
File metadata and controls
36 lines (26 loc) · 945 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import time
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub, SubscribeListener
# this will print out the subscription status to console
class Listener(SubscribeListener):
def status(self, pubnub, status):
print(f'Status: \n{status.category.name}')
# here we create configuration for our pubnub instance
config = PNConfiguration()
config.subscribe_key = 'demo'
config.publish_key = 'demo'
config.user_id = 'example'
config.enable_subscribe = True
config.daemon = True
pubnub = PubNub(config)
pubnub.add_listener(Listener())
subscription = pubnub.channel('example').subscription()
subscription.on_message = lambda message: print(f'Message from {message.publisher}: {message.message}')
subscription.subscribe()
time.sleep(1)
publish_result = pubnub.publish().channel("example").message("Hello from PubNub Python SDK").sync()
time.sleep(3)
pubnub.stop()
time.sleep(1)
print('Bye.')