hharnisc / python-meteor

A meteor client for python
MIT License
157 stars 27 forks source link

Running example #4

Closed benjiqq closed 10 years ago

benjiqq commented 10 years ago

I tried to get python-meteor to run but its not working so far. I'm trying this example from the mailing list (see error below). Is there some code which runs with a meteor example app? thanks

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('lists')
# todos needs a list _id to subscribe to
# client.subscribe('todos')

# (sort of) hacky way to keep the client alive
# ctrl + c to kill the script
while True:
    try:
        client.ddp_client.run_forever()
    except KeyboardInterrupt:
        break
* CONNECTED
Traceback (most recent call last):
  File "example.py", line 51, in <module>
    client.ddp_client.run_forever()
AttributeError: 'DDPClient' object has no attribute 'run_forever'
hharnisc commented 10 years ago

I've added a sample script

https://github.com/hharnisc/python-meteor/blob/master/example.py

and some documentation in the readme

https://github.com/hharnisc/python-meteor#example