Django DDP: Meteor DDP Server Implementation for Django
Django DDP is a powerful tool that brings the capabilities of Meteor DDP (Distributed Data Protocol) to Django applications. By using Django DDP, developers can enable Meteor applications to subscribe to changes on Django models, allowing real-time data synchronization between the two frameworks. In this article, we will explore the features, installation, and usage of Django DDP, along with its scalability, limitations, and authentication capabilities.
Installation and Requirements
To use Django DDP, you need to have PostgreSQL with psycopg2 installed in your Django project. These dependencies provide the necessary asynchronous support required by Django DDP. Additionally, the test suite includes an example Meteor project, so you’ll need to have Meteor installed if you want to run the tests. The easiest way to install Django DDP is through pip
:
pip install django-ddp
If you want to test the pre-release code, you can clone the development version directly from GitHub:
pip install -e git+https://github.com/commoncode/django-ddp@develop#egg=django-ddp
Getting Started
Once installed, getting started with Django DDP is straightforward. The first step is to add 'dddp'
to the INSTALLED_APPS
in your project settings file. This will register the necessary handlers for Django signals on model save/update operations.
Next, each Django application in your project needs to register collections and publications via ddp
sub-modules. These modules contain the definitions for collections and publications that Meteor applications can subscribe to. You can add a dddp.py
file inside your application module to register these collections and publications.
Clients can then subscribe to publications, and when models are saved, Django DDP will send change messages to the clients subscribed to relevant publications. It’s important to use the appropriate save()
and delete()
methods in your application code to ensure that the signals are raised and change messages are sent when models are modified.
Scalability and Limitations
Django DDP is designed to be highly scalable. All database queries to support DDP events are done once by the server instance that made the changes using the Django ORM. Django DDP then multiplexes messages for active subscriptions, broadcasting an aggregated change message on channels specific to each Django model that has been published. Peer servers can subscribe to these aggregate broadcast events, which are then dispatched to individual client connections. This approach eliminates the need for additional database queries for demultiplexing or dispatch by peer servers.
However, it’s important to note that Django DDP currently lacks support for the SockJS XHR fallback protocol. This means that browsers that don’t support WebSockets will not be able to use Django DDP. However, the number of browsers that don’t support WebSockets is minimal, with Opera Mini being the only notable exception. Additionally, Django DDP relies on Django signals for receiving model save/update signals, so changes must be made via the Django ORM. While there are no technical limitations preventing the use of database triggers, this feature is not currently implemented.
Advanced Usage
Django DDP provides additional features and capabilities for advanced usage scenarios. One option is to use Django DDP as a secondary DDP connection, allowing rapid development through the hot code push features provided by Meteor. In this configuration, your Meteor application can connect to the Django DDP service and subscribe to Django models through local collections.
Alternatively, you can configure Django DDP to be the primary DDP connection, eliminating the need for two separate DDP connections. Simply set the DDP_DEFAULT_CONNECTION_URL
environment variable to use Django DDP as the primary DDP connection in your Meteor application. This approach is recommended, especially if you want to use dddp.accounts
to provide authentication using django.contrib.auth
to your Meteor app.
Serving Meteor Applications from Django DDP
Django DDP can also serve your Meteor applications directly. To do this, you need to build your Meteor app into a directory and add a MeteorView to your urls.py
. The MeteorView will direct incoming requests to the Meteor application bundle. This allows you to serve your Meteor applications directly from Django DDP, simplifying the deployment and management of your applications.
Adding API Endpoints
Django DDP allows you to add API endpoints to your Django app. By calling the register
method of the dddp.api.API
object, you can define collections and publications that expose specific data and functionality to connected Meteor applications. The dddp.api.Collection
and dddp.api.Publication
classes are designed for this purpose, but you can also create your own subclasses of dddp.api.APIMixin
to define custom API endpoints.
Authentication
Authentication in Django DDP is provided using the standard Meteor accounts system along with the tysonclugg:accounts-secure
package. This package enhances security by using TLS (HTTPS + WebSockets) for all data transmission, protecting your site against man-in-the-middle and replay attacks. To enable authentication, simply add 'dddp.accounts'
to your INSTALLED_APPS
and follow the standard procedure for adding login/logout views to your Meteor application.
Conclusion
Django DDP is a powerful tool for integrating Meteor and Django applications, enabling real-time synchronization of data between the two frameworks. By following the installation and usage guidelines provided in this article, you can easily incorporate the capabilities of Meteor DDP into your Django projects. With its scalability, authentication features, and extensive API capabilities, Django DDP opens up a wide range of possibilities for building sophisticated and interactive web applications.
If you have any questions or want to learn more about Django DDP, feel free to reach out and ask. Happy coding!
References
- Django DDP Repository: https://github.com/django-ddp/django-ddp
- Django DDP Documentation: https://github.com/django-ddp/django-ddp/raw/master/README.rst
- Django Documentation: https://www.djangoproject.com/
- Meteor Documentation: https://www.meteor.com/
- PostgreSQL: http://postgresql.org/
- WebSockets: http://www.w3.org/TR/websockets/
Contributors
- Tyson Clugg: Author, conceptual design.
- Yan Le: Validate and bug fix dddp.accounts submodule.
- MEERQAT: Project sponsor.
- David Burles: Expert guidance on how DDP works in Meteor.
- Brenton Cleeland: Contributions to limiting visibility of published documents.
- Muhammed Thanish: Making the DDP Test Suite available.
- Common Code: Awesome team support and respect.
Leave a Reply