Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

25 Commits

Repository files navigation

python-ddp

An event driven ddp client

Installation

$ pip install python-ddp

Table of Contents

History

Latest Version 0.1.5

Version 0.1.4

Version 0.1.3

Version 0.1.2

  • BUGFIX - auto reconnect can now handle WebSocketExceptions (thanks @ppettit)

Version 0.1.1

  • Implemented auto reconnect (auto reconnect on by default) and reconnected event emitter

Version 0.1.0

  • Initial implementation, add ability to call, subscribe and unsubscribe

Quick Start

General Commands

Establish A Connection And Close It

fromDDPClientimportDDPClientclient=DDPClient('ws://127.0.0.1:3000/websocket')
client.connect()
client.close()

Establish A Connection Without Auto Reconnect

fromDDPClientimportDDPClientclient=DDPClient('ws://127.0.0.1:3000/websocket', auto_reconnect=False)
client.connect()

Establish A Connection And With Reconnect Different Frequency

fromDDPClientimportDDPClient# try to reconnect every secondclient=DDPClient('ws://127.0.0.1:3000/websocket', auto_reconnect=True, auto_reconnect_timeout=1)
client.connect()

Call A Remote Function

fromDDPClientimportDDPClientdefcallback_function(data):
printdataclient=DDPClient('ws://127.0.0.1:3000/websocket')
client.connect()
client.call('someFunction', [1,2,3], callback_function)

Subscribe and Unsubscribe

fromDDPClientimportDDPClientdefsubscription_callback(data):
printdataclient=DDPClient('ws://127.0.0.1:3000/websocket')
client.connect()
sub_id=client.subscribe('posts', subscription_callback)
client.unsubscribe(sub_id)

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

####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)

Events and Callback Arguments

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:

fromDDPClientimportDDPClientclient=DDPClient('ws://127.0.0.1:3000/websocket')

connected

Register the event to a callback function

defconnected(self):
print'* CONNECTED'client.on('connected', connected)

The connected event callback takes no arguments

socket_closed

Register the event to a callback function

defclosed(self, code, reason):
print'* CONNECTION CLOSED {} {}'.format(code, reason)
client.on('socket_closed', closed)

socket_closed callback takes the following arguments

code - the error code
reason - the error message

reconnected

defreconnected(self):
print'* RECONNECTED'client.on('reconnected', reconnected)

reconnected call back takes no arguments

failed

Register the event to a callback function

deffailed(collection, data):
print'* FAILED - data: {}'.format(str(data))
client.on('failed', failed)

failed callback takes the following arguments

data - the error data

version_mismatch

Register the event to a callback function

This event is fired if the server and client can not agree on a DDP version to use and is a fatal error

defversion_mismatch(versions):
print'* VERSION MISMATCH - versions: {}'.format(str(versions))
client.on('version_mismatch', version_mismatch)

version_mismatch callback takes the following arguments

versions - the DDP versions attempted

added

Register the event to a callback function

defadded(collection, id, fields):
print'* ADDED {} {}'.format(collection, id)
forkey, valueinfields.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

changed

Register the event to a callback function

defchanged(self, collection, id, fields, cleared):
print'* CHANGED {} {}'.format(collection, id)
forkey, valueinfields.items():
print' - FIELD {} {}'.format(key, value)
forkey, valueincleared.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

removed

Register the event to a callback function

defremoved(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('reconnected', reconnected)
client.on('failed', failed)
client.on('version_mismatch', version_mismatch)
client.on('added', added)
client.on('changed', changed)
client.on('removed', removed)

##Collaborators

About

An event driven ddp client

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages