Skip to content

Commit 168bb9a

Browse files
committed
add an example script
1 parent db52ce2 commit 168bb9a

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
venv
22
dist
3-
*.egg-info
3+
*.egg-info
4+
*.pyc

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $ pip install python-meteor
1414
- [TODO](#TODO)
1515
- [Quick Start](#quick-start)
1616
- [Usage](#usage)
17+
- [Example](#example)
1718
- [Collaborators](#collaborators)
1819

1920
## History
@@ -509,6 +510,23 @@ client.on('logged_in', logged_in)
509510
client.on('logged_out', logged_out)
510511
```
511512

513+
##Example
514+
515+
There is an included `example.py` script to use with the `todo` sample app included with meteor
516+
517+
Create the sample meteor app and start it
518+
519+
```bash
520+
$ meteor create --example todos
521+
$ meteor
522+
```
523+
524+
Then run example.py
525+
526+
```bash
527+
$ python example.py
528+
```
529+
512530
##Collaborators
513531

514532
- [@ppettit](https://github.com/ppettit)

example.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import time
2+
3+
from MeteorClient import MeteorClient
4+
5+
client = MeteorClient('ws://127.0.0.1:3000/websocket')
6+
7+
def subscribed(subscription):
8+
print '* SUBSCRIBED {}'.format(subscription)
9+
10+
def unsubscribed(subscription):
11+
print '* UNSUBSCRIBED {}'.format(subscription)
12+
13+
def added(collection, id, fields):
14+
print '* ADDED {} {}'.format(collection, id)
15+
for key, value in fields.items():
16+
print ' - FIELD {} {}'.format(key, value)
17+
18+
# query the data each time something has been added to
19+
# a collection to see the data `grow`
20+
all_lists = client.find('lists', selector={})
21+
print 'Lists: {}'.format(all_lists)
22+
print 'Num lists: {}'.format(len(all_lists))
23+
24+
25+
# if collection == 'list' you could subscribe to the list here
26+
# with something like
27+
# client.subscribe('todos', id)
28+
# all_todos = client.find('todos', selector={})
29+
# print 'Todos: {}'.format(all_todos)
30+
31+
def connected():
32+
print '* CONNECTED'
33+
34+
def subscription_callback(error):
35+
if error:
36+
print error
37+
38+
client.on('subscribed', subscribed)
39+
client.on('unsubscribed', unsubscribed)
40+
client.on('added', added)
41+
client.on('connected', connected)
42+
43+
client.connect()
44+
client.subscribe('lists')
45+
46+
47+
# (sort of) hacky way to keep the client alive
48+
# ctrl + c to kill the script
49+
while True:
50+
try:
51+
time.sleep(1)
52+
except KeyboardInterrupt:
53+
break

0 commit comments

Comments
 (0)