-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathsubscription_set.py
More file actions
97 lines (72 loc) · 2.92 KB
/
Copy pathsubscription_set.py
File metadata and controls
97 lines (72 loc) · 2.92 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import time
from os import getenv
from pubnub.callbacks import SubscribeCallback
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
# Listeners declaration
def on_message(message):
print(f"\033[94mMessage received on {message.channel}: \n{message.message}\033[0m")
def on_presence(presence):
print(f"\033[0;32mPresence event received on: {presence.subscription or presence.channel}: ",
f" \t{presence.uuid} {presence.event}s \033[0m")
class PrintListener(SubscribeCallback):
def status(self, _, status):
print(f'\033[1;31mPrintListener.status:\n{status.category.name}\033[0m')
def presence(self, _, presence):
print(f"\033[0;32mPresence event received on: {presence.subscription or presence.channel}: ",
f" \t{presence.uuid} {presence.event}s \033[0m")
channel = 'test'
config = PNConfiguration()
config.subscribe_key = getenv("PN_KEY_SUBSCRIBE")
config.publish_key = getenv("PN_KEY_PUBLISH")
config.user_id = "example"
config.enable_subscribe = True
config.daemon = True
pubnub = PubNub(config)
pubnub.add_listener(PrintListener())
pubnub.add_channel_to_channel_group().channels(['test', 'test_in_group']).channel_group('group-test').sync()
# Subscribing
channel_1 = pubnub.channel(channel).subscription()
channel_2 = pubnub.channel(f'{channel}.2').subscription(with_presence=True)
channel_x = pubnub.channel(f'{channel}.*').subscription(with_presence=True)
channel_x.on_message = lambda message: print(f"\033[96mWildcard {message.channel}: \n{message.message}\033[0m")
group = pubnub.channel_group('group-test').subscription()
group.on_message = lambda message: print(f"\033[96mChannel Group {message.channel}: \n{message.message}\033[0m")
subscription_set = pubnub.subscription_set([channel_1, channel_2, channel_x, group])
subscription_set.on_message = on_message
subscription_set.on_presence = on_presence
set_subscription = subscription_set.subscribe()
time.sleep(1)
# Testing message delivery
publish_result = pubnub.publish() \
.channel(f'{channel}') \
.message('Hello channel "test" from PubNub Python SDK') \
.meta({'lang': 'en'}) \
.sync()
time.sleep(1)
publish_result = pubnub.publish() \
.channel(f'{channel}.2') \
.message('PubNub Python SDK の Hello チャンネル「test」') \
.meta({'lang': 'ja'}) \
.sync()
time.sleep(1)
publish_result = pubnub.publish() \
.channel(f'{channel}.3') \
.message('PubNub Python SDK mówi cześć') \
.meta({'lang': 'pl'}) \
.sync()
time.sleep(1)
time.sleep(1)
publish_result = pubnub.publish() \
.channel(f'{channel}_in_group') \
.message('Hola desde el SDK de Python de Pubnub.') \
.meta({'lang': 'es'}) \
.sync()
time.sleep(1)
print('Removing subscription object for "test"')
pubnub.remove_channel_from_channel_group().channels(['test']).channel_group('group-test').sync()
time.sleep(1)
subscription_set.unsubscribe()
print('Exiting')
pubnub.stop()
exit(0)