Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae7dbde

Browse filesBrowse files
author
gcohen
committed
Added verbose comments, and added a scenarion to depict the client trying to subscribe on a restricted presence channel.
1 parent 19019a3 commit ae7dbde
Copy full SHA for ae7dbde

File tree

Expand file treeCollapse file tree

1 file changed

+52
-22
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+52
-22
lines changed
Open diff view settings
Collapse file

‎python/examples/pam_demo/demo.py‎

Copy file name to clipboard
+52-22Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from gevent.monkey import patch_all
32
patch_all()
43

@@ -13,62 +12,93 @@ def get_unique(s):
1312
return 'str-' + rand + '-' + s
1413

1514
# public channel
15+
# This is the channel all clients announce themselves on -- or more generally speaking, a channel you expect the client
16+
# to "check-in" on to announce his state
17+
1618
channel_public = get_unique("channel_public")
1719

1820
# server auth key
21+
# Only the server has/knows about this auth token. It will be used to grant read on the "check-in" Presence channel
22+
1923
server_auth_token = get_unique("server_auth_token")
2024

21-
#client auth key
25+
# client auth key
26+
# only clients will use this authey -- it does not provide presence channel read access
27+
2228
client_auth_token = get_unique("client_auth_token")
2329

24-
#client uuid
30+
# each client must have a unique id -- a UUID, for presence information/state to bind to
31+
32+
# client uuid
2533
client_uuid = get_unique("client_uuid")
2634

27-
#server uuid
35+
# server uuid
2836
server_uuid = get_unique("server_uuid")
2937

38+
# For the demo, we'll implement a SERVER called server, who is the authoritative 'admin' entity in the system
39+
# We'll also implement a CLIENT called client, who is an arbitrary hardware device member of the network
3040

31-
#init server object
41+
# Please swap out the default 'pam' demo keys with your own PAM-enabled keys
42+
43+
# init server object
3244
server = Pubnub(publish_key="pam", subscribe_key="pam", secret_key="pam", auth_key=server_auth_token, uuid=server_uuid)
3345

34-
#init client object
46+
# init client object
3547
client = Pubnub(publish_key="pam", subscribe_key="pam", auth_key=client_auth_token, uuid=client_uuid)
3648

49+
# To access a Presence channel with PAM, its format is CHANNELNAME-pnpres
50+
3751
# Grant permission to server auth keys
52+
# grant r/w to public, and r/w public Presence (public-pnpres)
53+
3854
print(server.grant(channel=channel_public, auth_key=server_auth_token, read=True, write=True))
3955
print(server.grant(channel=channel_public + '-pnpres', auth_key=server_auth_token, read=True, write=True))
4056

4157
# Grant permission to client auth keys
58+
# grant r/w to public, and w-only access to public Presence (public-pnpres)
4259
print(server.grant(channel=channel_public, auth_key=client_auth_token, read=True, write=False))
43-
print(server.grant(channel=channel_public + '-pnpres', auth_key=client_auth_token, read=True, write=False))
60+
print(server.grant(channel=channel_public + '-pnpres', auth_key=client_auth_token, read=False, write=False))
4461

62+
# Now, we'll run it to watch it work as advertised...
4563

64+
# Define some simple callabcks for the Server and Client
4665

47-
def _server_callback(message, channel):
48-
print(message)
66+
def _server_message_callback(message, channel):
67+
print("Server heard: " + message)
68+
69+
def _client_callback(channel, message):
70+
print("Client heard: " + message)
4971

5072
def _error_callback(error):
51-
print(error)
73+
print("Error: " + error)
5274

53-
#server subscribes to public channel
54-
server.subscribe(channels=channel_public, callback=_server_callback, error=_error_callback)
75+
def _server_presence_callback(message, channel):
76+
print message
77+
if 'action' in message:
78+
if message['action'] == 'join' and message['uuid'] == client_uuid:
79+
print "Server can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data'])
5580

81+
def _client_presence_callback(message, channel):
82+
print message
83+
if 'action' in message:
84+
if message['action'] == 'join' and message['uuid'] == client_uuid:
85+
print "Client can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data'])
5686

87+
# server subscribes to public channel
88+
server.subscribe(channels=channel_public, callback=_server_message_callback, error=_error_callback)
5789

58-
def _server_presence_callback(message, channel):
59-
print message
60-
if 'action' in message:
61-
if message['action'] == 'join' and message['uuid'] == client_uuid:
62-
print "Only I can see that client with UUID " + message['uuid'] + " has a state of " + json.dumps(message['data'])
90+
# server subscribes to presence events on public channel
91+
# presence() is a convienence method that subscribes to channel-pnpres with special logic for handling
92+
# presence-event formatted messages
6393

64-
# server subscribes to presence events on public channel
94+
## uncomment out to see server able to read on presence channel
6595
server.presence(channel=channel_public, callback=_server_presence_callback, error=_error_callback)
6696

97+
# now if the client tried to subscribe on the presence channel, and therefore, get state info
98+
# he is explicitly denied!
6799

68-
69-
70-
def _client_callback(channel, message):
71-
print(message)
100+
## uncomment out to see client not able to read on presence channel
101+
#client.presence(channel=channel_public, callback=_client_presence_callback, error=_error_callback)
72102

73103
# client subscribes to public channel
74104
client.subscribe(channels=channel_public, state={ "myKey" : get_unique("foo")}, callback=_client_callback, error=_error_callback)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.