An event driven ddp client
Installation
$ pip install python-ddp
Table of Contents
Latest Version 0.1.0
- Initial implementation, add ability to call, subscribe and unsubscribe
Establish a connection and Close It
from DDPClient import DDPClient
client = DDPClient('ws://127.0.0.1:3000/websocket')
client.connect()
client.close()
Call A Remote Function
from DDPClient import DDPClient
def callback_function(data):
print data
client = DDPClient('ws://127.0.0.1:3000/websocket')
client.connect()
client.call('someFunction', [1,2,3], callback_function)
Subscribe and Unsubscribe
from DDPClient import DDPClient
def subscription_callback(data):
print data
client = DDPClient('ws://127.0.0.1:3000/websocket')
client.connect()
sub_id = client.subscribe('posts', subscription_callback)
client.unsubscribe(sub_id)
Connected
from DDPClient import DDPClient
def connected(self):
print '* CONNECTED'
client = DDPClient('ws://127.0.0.1:3000/websocket')
client.on('connected', connected)
client.connect()
Socket Closed
from DDPClient import DDPClient
def closed(self, code, reason):
print '* CONNECTION CLOSED {} {}'.format(code, reason)
client = DDPClient('ws://127.0.0.1:3000/websocket')
client.on('socket_closed', closed)
client.connect()
Added To Collection
from DDPClient import DDPClient
def added(collection, id, fields):
print '* ADDED {} {}'.format(collection, id)
for key, value in fields.items():
print ' - FIELD {} {}'.format(key, value)
client = DDPClient('ws://127.0.0.1:3000/websocket')
client.on('added', added)
client.connect()
sub_id = client.subscribe('posts')
Collection Changed
from DDPClient import DDPClient
def changed(self, 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 = DDPClient('ws://127.0.0.1:3000/websocket')
client.on('changed', changed)
client.connect()
sub_id = client.subscribe('posts')
Removed From Collection
from DDPClient import DDPClient
def removed(collection, id):
print '* REMOVED {} {}'.format(collection, id)
client = DDPClient('ws://127.0.0.1:3000/websocket')
client.on('removed', removed)
client.connect()
sub_id = client.subscribe('posts')
Failed
from DDPClient import DDPClient
def failed(collection, code, reason):
print '* FAILED - data: {}'.format(str(data))
client = DDPClient('ws://127.0.0.1:3000/websocket')
client.connect()
client.on('failed', failed)
####call(self, method, params, callback=None)
Call a method on the server
Arguments
method - the remote server method
params - an array of commands to send to the method
Keyword Arguments
callback - a callback function containing the return data
####subscribe(self, name, params, callback=None)
Subcribe to add/change/remove events for a collection
Arguments
name - the name of the publication to subscribe
params - params to subscribe (parsed as json)
Keyword Arguments
callback - a callback function that gets executed when the subscription has completed
####unsubscribe(self, sub_id)
Unsubscribe from a collection
Arguments
sub_id - the id of the subsciption (returned by subcribe)
When creating an instance of DDPClient
it is capable of emitting a few events with arguments. The documentation below assumes that you've instanciated a client with the following code:
from DDPClient import DDPClient
client = DDPClient('ws://127.0.0.1:3000/websocket')
Register the event to a callback function
def connected(self):
print '* CONNECTED'
client.on('connected', connected)
The connected event callback takes no arguments
Register the event to a callback function
def closed(self, code, reason):
print '* CONNECTION CLOSED {} {}'.format(code, reason)
client.on('socket_closed', closed)
closed
callback takes the following arguments
code - the error code
reason - the error message
Register the event to a callback function
def failed(collection, code, reason):
print '* FAILED - data: {}'.format(str(data))
client.on('failed', failed)
failed
callback takes the following arguments
code - the error code
reason - the error message
Register the event to a callback function
def 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
Register the event to a callback function
def changed(self, 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
Register the event to a callback function
def 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
####All of the callbacks
For reference
client.on('connected', connected)
client.on('socket_closed', closed)
client.on('failed', failed)
client.on('added', added)
client.on('changed', changed)
client.on('removed', removed)