Package: django-ddp
Django DDPis aDjango/PostgreSQLimplementation of the Meteor DDP
server,  allowingMeteorto subscribe to changes onDjangomodels.
Released under the MIT license.RequirementsYou must be usingPostgreSQLwithpsycopg2in yourDjangoproject
for django-ddp to work.  There is no requirement on any asynchronous
framework such as Redis or crossbar.io as they are simply not needed
given the asynchronous support provided byPostgreSQLwithpsycopg2.Since the test suite includes an exampleMeteorproject, running that
requires thatMeteoris installed (andmeteoris in yourPATH).InstallationInstall the latest release from pypi (recommended):pipinstalldjango-ddpClone and use development version direct from GitHub to test pre-release
code (no GitHub account required):pipinstall-egit+https://github.com/commoncode/django-ddp@develop#egg=django-ddpOverview and getting startedDjango DDP registers handlers forDjango signalson all model save/update operations.Add'dddp'to INSTALLED_APPS in your project settings file.Each Django application (ie: your code) registers Collections and Publications viaddpsub-modules for all INSTALLED_APPS.Register collections and publications in a file nameddddp.pyinside your application module.Clients subscribe to publications, entries are written into thedddp.Subscriptionanddddp.SubscriptionCollectionmodel tables and theget_queriesmethod of publications are called to retrieve the Django ORM queries that contain the objects that will be sent to the client.Runmanage.py migrateto update your database so it has the necessary tables needed for tracking client subscriptions.When models are saved, the Django DDP signal handlers send change messages to clients subscribed to relevant publications.Use the modelsave()anddelete()methods as appropriate in your application code so that appropriate signals are raised and change messages are sent.Geventis used to run WebSocket connections concurrently along with any Django views defined in your project (via your projecturls.py).Run your application using thedddpcommand which sets up the gevent mainloop and serves your Django project views.  This command takes care of routing WebSocket connections according to the URLs that Meteor uses, do not add URLs for WebSocket views to your projecturls.py.ScalabilityAll database queries to support DDP events are done once by the server
instance that has made changes via the Django ORM.  Django DDP multiplexes
messages for active subscriptions, broadcasting an aggregated change
message on channels specific to each Django model that has been published.Peer servers subscribe to aggregate broadcast events which are
de-multiplexed and dispatched to individual client connections.
No additional database queries are required for de-multiplexing
or dispatch by peer servers.LimitationsNo support for the SockJS XHR fallback protocol to support browsers
that don’t haveWebSockets(seehttp://caniuse.com/websocketsfor
supported browsers).  It is noteworthy that the only current browser
listed that doesn’t supportWebSocketsis Opera Mini, which doesn’t
support pages that use EcmaScript (JavaScript) for interactivity
anyway.  Offering SockJS XHR fallback wouldn’t help to substantially
increase browser support: if Opera Mini is excluded then all current
browser versions including IE, Edge, Firefox, Chrome, Safari, Opera,
iOS Safari, Android Browser Android and Chrome for Android are
supported.  Having said all that, pull requests are welcome.Changes must be made via the Django ORM as django-ddp usesDjango
signalsto receive model save/update signals.  There are no
technical reasons why database triggers couldn’t be used - pull
requests are welcome.Example usageAdd ‘dddp’ to your settings.INSTALLED_APPS:# settings.py...INSTALLED_APPS=list(INSTALLED_APPS)+['dddp']If you’d like support for the Meteor Accounts package (ie: login/logout
with django.contrib.auth) consult the section on authentication below
and use the following line instead:# settings.py...INSTALLED_APPS=list(INSTALLED_APPS)+['dddp','dddp.accounts']Add ddp.py to your Django application:# bookstore/ddp.pyfromdddp.apiimportAPI,Collection,PublicationfrombookstoreimportmodelsclassBook(Collection):model=models.BookclassAuthor(Collection):model=models.AuthorclassAllBooks(Publication):queries=[models.Author.objects.all(),models.Book.objects.all(),]classBooksByAuthorEmail(Publication):defget_queries(self,author_email):return[models.Author.objects.filter(email=author_email,),models.Book.objects.filter(author__email=author_email,),]API.register([Book,Author,AllBooks,BooksByAuthorEmail])Start the Django DDP service:DJANGO_SETTINGS_MODULE=myproject.settingsdddpUsing django-ddp as a secondary DDP connection (RAPID DEVELOPMENT)Running in this manner allows rapid development through use of the hot
code push features provided by Meteor.Connect your Meteor application to the Django DDP service:// bookstore.jsif(Meteor.isClient){// Connect to Django DDP serviceDjango=DDP.connect('http://'+window.location.hostname+':8000/');// Create local collections for Django models received via DDPAuthors=newMongo.Collection("bookstore.author",{connection:Django});Books=newMongo.Collection("bookstore.book",{connection:Django});// Subscribe to all books by Janet EvanovichDjango.subscribe('BooksByAuthorEmail','janet@evanovich.com');}Start Meteor (from within your meteor application directory):meteorUsing django-ddp as the primary DDP connection (RECOMMENDED)If you’d prefer to not have two DDP connections (one to Meteor and one
to django-ddp) you can set theDDP_DEFAULT_CONNECTION_URLenvironment
variable to use the specified URL as the primary DDP connection in
Meteor.  When doing this, you won’t need to useDDP.connect(…)or
specify{connection: Django}on your collections.  Running with
django-ddp as the primary connection is recommended, and indeed required
if you wish to usedddp.accountsto provide authentication usingdjango.contrib.authto your meteor app.DDP_DEFAULT_CONNECTION_URL=http://localhost:8000/meteorServing your Meteor applications from django-ddpFirst, you will need to build your meteor app into a directory (examples
below assume target directory namedmyapp):meteorbuild../myappThen, add a MeteorView to your urls.py:fromdddp.viewsimportMeteorViewurlpatterns=patterns(url('^(?P<path>/.*)$',MeteorView.as_view(json_path=os.path.join(settings.PROJ_ROOT,'myapp','bundle','star.json',),),)Adding API endpoints (server method definitions)API endpoints can be added by callingregistermethod of the
dddp.api.API object from the ddp.py module of your Django app, on a
subclass of dddp.api.APIMixin - both dddp.api.Collection and
dddp.api.Publication are suitable, or you may define your own subclass
of dddp.api.APIMixin.  A good example of this can be seen in
dddp/accounts/ddp.py in the source of django-ddp.AuthenticationAuthentication is provided using the standard meteor accounts system,
along with theaccounts-securepackage which turns off Meteor’s
password hashing in favour of using TLS (HTTPS + WebSockets). This
ensures strong protection for all data over the wire.  Correctly using
TLS/SSL also protects your site against man-in-the-middle and replay
attacks - Meteor is vulnerable to both of these without using
encryption.Adddddp.accountsto yoursettings.INSTALLED_APPSas described in
the example usage section above, then addtysonclugg:accounts-secureto your Meteor application (from within your meteor application
directory):meteoraddtysonclugg:accounts-secureThen follow the normal procedure to add login/logout views to your
Meteor application.ContributorsTyson CluggAuthor, conceptual design.Yan LeValidate and bug fix dddp.accounts submodule.MEERQATProject sponsor - many thanks for allowing this to be released under an open source license!David BurlesExpert guidance on how DDP works in Meteor.Brenton CleelandGreat conversations around how collections and publications can limit visibility of published documents to specific users.Muhammed ThanishMaking theDDP Test Suiteavailable.This project is forever grateful for the love, support and respect given
by the awesome team atCommon Code.