Package: python-meteor
# python-meteorAn event driven Meteor client**Installation**```bash$ pip install python-meteor```**Table of Contents**- [History](#history)- [TODO](#TODO)- [Quick Start](#quick-start)- [Usage](#usage)- [Example](#example)- [Collaborators](#collaborators)## History**Latest Version** 0.1.6- Add support for meteor login resume tokens (thanks [@ppettit](https://github.com/ppettit))**Version** 0.1.5- BUGFIX - unsubscribe was not unsubcribing (missing sub ID) (thanks [@tdamsma](https://github.com/tdamsma))- examples and docs support python 3**Version** 0.1.4- BUGFIX - update connected status when reconnecting (thanks [@ppettit](https://github.com/ppettit))- BUGFIX - make sure `logged_in` callback get's fired (thanks [@pmgration](https://github.com/pmgration))- NOTE: python-ddp library has been updated that addresses connection problems**Version** 0.1.3- Fixed a bug that was causing a crash while removing a field on a change event (thanks [@ppettit](https://github.com/ppettit))**Version** 0.1.2- Implemented auto reconnect (auto reconnect on by default) and reconnected event emitter**Version** 0.1.1- Fixed bug in setup, was including built in hashlib**Version** 0.1.0- Initial implementation, add ability to call, subscribe, unsubscribe, do basic queries and login- Data is stored in a local python dictionary (in memory) and updated in real time as collection change events are received. This allows for very a basic find and find_one APIs to be implemented.## TODO- Full minimongo API for find and find_one- CI unit testing with Travis## Quick Start### General Commands**Create a Meteor client and connect**```pythonfrom MeteorClient import MeteorClientclient = MeteorClient('ws://127.0.0.1:3000/websocket')client.connect()```**Establish A Connection Without Auto Reconnect**```pythonfrom MeteorClient import MeteorClientclient = MeteorClient('ws://127.0.0.1:3000/websocket', auto_reconnect=False)client.connect()```**Establish A Connection And With Reconnect Different Frequency**```pythonfrom MeteorClient import MeteorClient# try to reconnect every secondclient = MeteorClient('ws://127.0.0.1:3000/websocket', auto_reconnect=True, auto_reconnect_timeout=1)client.connect()```**Call a remote function**```pythondef callback_function(error, result):if error:print(error)returnprint(result)client.call('someFunction', [1, 2, 3], callback_function)```**Subscribe and Unsubscribe**```pythondef subscription_callback(error):if error:print(error)client.subscribe('posts', callback=subscription_callback)client.unsubscribe('posts')```**Find All Data In a Collection**```pythonall_posts = client.find('posts')```**Find Data In a Collection With Selector**```pythonsacha_posts = client.find('posts', selector={'author': 'Sacha Greif'})```**Find One**```pythonone_post = client.find_one('posts')```**Fine One With Selector**```pythonone_post = client.find_one('posts', selector={'author': 'Sacha Greif'})```**Insert**```pythondef insert_callback(error, data):if error:print(error)returnprint(data)client.insert('posts', {'title': 'Google', 'url': 'https://google.com', 'comments': 'Search'}, callback=insert_callback)```**Update**```pythondef update_callback(error, data):if error:print(error)returnprint(data)client.update('posts', {'title': 'Google'}, {'comments': 'Google main page'}, callback=update_callback)```**Remove**```pythondef remove_callback(error, data):if error:print(error)returnprint(data)client.remove('posts', {'title': 'Google'}, callback=remove_callback)```## Usage### Class Init####DDPClient(url, auto_reconnect=True, auto_reconnect_timeout=0.5, debug=False)**Arguments**_url_ - to connect to ddp server**Keyword Arguments**_auto_reconnect_ - automatic reconnect (default: True)_auto_reconnect_timeout_ - reconnect every X seconds (default: 0.5)_debug_ - print out lots of debug info (default: False)### Functions####connect()Connect to the meteor server####login(user, password, token=token, callback=None)Login with a username and password. If a token is provided it will be tried first, falling back to username and password ifthe token is invalid.**Arguments**_user_ - username or email address_password_ - the password for the account**Keyword Arguments**_token_ - meteor resume token_callback_ - callback function containing error as first argument and login data####logout(callback=None)Logout a user**Keyword Arguments**_callback_ - callback function called when the user has been logged out#### call(method, params, callback=None)Call a remote method**Arguments**_method_ - remote method name_params_ - remote method parameters**Keyword Arguments**_callback_ - callback function containing return data#### subscribe(name, params=[], callback=None)Subscribe to a collection**Arguments**_name_ - the name of the publication_params_ - the subscription parameters**Keyword Arguments**_callback_ - a function callback that returns an error (if exists)####unsubscribe(name)Unsubscribe from a collection**Arguments**_name_ - the name of the publication####find(collection, selector={})Find data in a collection**Arguments**_collection_ - collection to search**Keyword Arguments**_selector_ - the query (default returns all items in a collection)####find_one(collection, selector={})Return one item from a collection**Arguments**_collection_ - collection to search**Keyword Arguments**_selector_ - the query (default returns first item found)####insert(collection, doc, callback=None)Insert an item into a collection**Arguments**_collection_ - the collection to be modified_doc_ - The document to insert. May not yet have an _id attribute,in which case Meteor will generate one for you.**Keyword Arguments**_callback_ - Optional. If present, called with an error object as the first argument and,if no error, the _id as the second.####update(collection, selector, modifier, callback=None)Insert an item into a collection**Arguments**_collection_ - the collection to be modified_selector_ - specifies which documents to modify_modifier_ - Specifies how to modify the documents**Keyword Arguments**_callback_ - Optional. If present, called with an error object as the first argument and,if no error, the number of affected documents as the second.####remove(collection, selector, callback=None)Remove an item from a collection**Arguments**_collection_ - the collection to be modified_selector_ - Specifies which documents to remove**Keyword Arguments**_callback_ - Optional. If present, called with an error object as its argument.### Events and Callback ArgumentsWhen creating an instance of `MeteorClient` it is capable of emitting a few events with arguments. The documentation below assumes that you've instanciated a client with the following code:```pythonfrom MeteorClient import MeteorClientclient = MeteorClient('ws://127.0.0.1:3000/websocket')```#### connectedRegister the event to a callback function```pythondef connected(self):print('* CONNECTED')client.on('connected', connected)```The connected event callback takes no arguments#### closedRegister the event to a callback function```pythondef closed(self, code, reason):print('* CONNECTION CLOSED {} {}'.format(code, reason))client.on('closed', closed)````closed` callback takes the following arguments_code_ - the error code_reason_ - the error message### reconnected```pythondef reconnected(self):print('* RECONNECTED')client.on('reconnected', reconnected)````reconnected` call back takes no arguments#### failedRegister the event to a callback function```pythondef failed(collection, data):print('* FAILED - data: {}'.format(str(data)))client.on('failed', failed)````failed` callback takes the following arguments_data_ - the error data#### addedRegister the event to a callback function```pythondef added(collection, id, fields):print('* ADDED {} {}'.format(collection, id))for key, value in fields.items():print('  - FIELD {} {}'.format(key, value))client.on('added', added)````added` callback takes the following arguments_collection_ - the collection that has been modified_id_ - the collection item id_fields_ - the fields for item#### changedRegister the event to a callback function```pythondef changed(collection, id, fields, cleared):print('* CHANGED {} {}'.format(collection, id))for key, value in fields.items():print('  - FIELD {} {}'.format(key, value))for key, value in cleared.items():print('  - CLEARED {} {}'.format(key, value))client.on('changed', changed)````changed` callback takes the following arguments_collection_ - the collection that has been modified_id_ - the collection item id_fields_ - the fields for item_cleared_ - the fields for the item that have been removed#### removedRegister the event to a callback function```pythondef removed(collection, id):print('* REMOVED {} {}'.format(collection, id))client.on('removed', removed)````removed` callback takes the following arguments_collection_ - the collection that has been modified_id_ - the collection item id#### subscribedRegister the event to a callback function```pythondef subscribed(subscription):print('* SUBSCRIBED {}'.format(subscription))client.on('subscribed', subscribed)````subscribed` callback takes the following arguments_subscription_ - the name of the subscription#### unsubscribedRegister the event to a callback function```pythondef unsubscribed(subscription):print('* UNSUBSCRIBED {}'.format(subscription))client.on('unsubscribed', unsubscribed)````unsubscribed` callback takes the following arguments_subscription_ - the name of the subscription#### logging_inRegister the event to a callback function```pythondef logging_in():print('* LOGGIN IN')client.on('logging_in', logging_in)````logging_in` callback takes no arguments#### logged_inRegister the event to a callback function```pythondef logged_in(data):print('* LOGGED IN {}'.format(data))client.on('logged_in', logged_in)````logged_in` callback takes the following arguments_data_ - login return data#### logged_outRegister the event to a callback function```pythondef logged_out():print('* LOGGED OUT')client.on('logged_out', logged_out)````logged_out` callback takes no arguments####All of the callbacksFor reference```pythonclient.on('connected', connected)client.on('socket_closed', closed)client.on('reconnected', reconnected)client.on('failed', failed)client.on('added', added)client.on('changed', changed)client.on('removed', removed)client.on('subscibed', subscibed)client.on('unsubscribed', unsubscribed)client.on('logging_in', logging_in)client.on('logged_in', logged_in)client.on('logged_out', logged_out)```##ExampleThere is an included `example.py` script to use with the `todo` sample app included with meteorCreate the sample meteor app and start it```bash$ meteor create --example todos$ meteor```Then run example.py```bash$ python example.py```##Collaborators- [@ppettit](https://github.com/ppettit)- [@pmgration](https://github.com/pmgration)- [@tdamsma](https://github.com/tdamsma)