-
Notifications
You must be signed in to change notification settings - Fork 27
/
example.py
59 lines (40 loc) · 1.39 KB
/
example.py
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
import time
from MeteorClient import MeteorClient
client = MeteorClient('ws://127.0.0.1:3000/websocket')
def subscribed(subscription):
print('* SUBSCRIBED {}'.format(subscription))
def unsubscribed(subscription):
print('* UNSUBSCRIBED {}'.format(subscription))
def added(collection, id, fields):
print('* ADDED {} {}'.format(collection, id))
for key, value in fields.items():
print(' - FIELD {} {}'.format(key, value))
# query the data each time something has been added to
# a collection to see the data `grow`
all_lists = client.find('lists', selector={})
print('Lists: {}'.format(all_lists))
print('Num lists: {}'.format(len(all_lists)))
# if collection == 'list' you could subscribe to the list here
# with something like
# client.subscribe('todos', id)
# all_todos = client.find('todos', selector={})
# print 'Todos: {}'.format(all_todos)
def connected():
print('* CONNECTED')
def subscription_callback(error):
if error:
print(error)
client.on('subscribed', subscribed)
client.on('unsubscribed', unsubscribed)
client.on('added', added)
client.on('connected', connected)
client.connect()
client.subscribe('publicLists')
# (sort of) hacky way to keep the client alive
# ctrl + c to kill the script
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
break
client.unsubscribe('publicLists')