|
| 1 | +# django-subscriptions-rxdb |
| 2 | + |
| 3 | +## tl;dr |
| 4 | +This example reproduces the server-side component of the RxDB GraphQL example, with a couple of extras, which can be found [here](https://github.com/pubkey/rxdb/tree/master/examples/graphql). Checkout RxDB and run the GraphQL example. When running Ok, change `export const GRAPHQL_PORT = 10102;` and `export const GRAPHQL_SUBSCRIPTION_PORT = 10103;` ([lines 1 and 3 of this file](https://github.com/pubkey/rxdb/blob/master/examples/graphql/shared.js)) to equal `8000`. Then in the directory of this README file, `make install && make run`. More information about RxDB and GraphQL synchronisation can be found [here](https://rxdb.info/replication-graphql.html). |
| 5 | + |
| 6 | +## Requirements |
| 7 | +- [Clone of RxDB](https://github.com/pubkey/rxdb) |
| 8 | +- [Python 3.8+](https://www.python.org/downloads/) (might work with other versions, not tested) |
| 9 | +- [Poetry](https://python-poetry.org/) |
| 10 | +- `make` |
| 11 | +- [PostgreSQL](https://www.postgresql.org/) (optional) |
| 12 | + |
| 13 | +Tested on Ubuntu 20.04, should work everywhere these are available with no changes. |
| 14 | + |
| 15 | +## Reason for this example |
| 16 | +While connectivity is getting better around the world every year, every user will always at least have moments when their connection is spotty, if not completely absent (at human prices) for certain periods (eg., on a plane). With an offline-first, local database and other offline-first tech (service workers, etc.), it is possible to develop web-technology-based applications that will continue to offer much of their functionality offline, with realtime sync when they are online. This really is the best of both worlds, and can give much more fluid and friendly usage when connections are spotty. |
| 17 | + |
| 18 | +So you already have a Django-based app, and want to add some rich, client-side, offline-first functionality without starting from scratch? This is an example of some of the server-side code for one way of starting out. |
| 19 | + |
| 20 | +The RxDB component of this is not included here to keep things as light as possible. Offline-first has complexities that need to be mastered, so this is not intended to be a template or even a particularly good way of doing things, just a rough example to get you started with `rxdb`, `strawberry`, `django` and `broadcaster`. |
| 21 | + |
| 22 | +### The full example stack |
| 23 | + |
| 24 | +The example shows how you can do offline-realtime synchronisation of a local, RxDB database with Django. It uses Strawberry GraphQL subscriptions over websockets for the realtime sync capabilities. The default Django database configured in the `settings` file is Sqlite3 but there is also commented-out config for PostgreSQL. |
| 25 | + |
| 26 | +If you use PostgreSQL then you will have to set up a database like you would for any PostgreSQL-powered Django site but if you do, the example will also then use PostgreSQL for the realtime notification functionality. To run the example with `postgres`, comment out the `sqlite3` config and put your `postgres` config in the settings file (src/demo/settings.py). You will also need to install extra deps with: |
| 27 | + |
| 28 | +``` |
| 29 | +make install-postgres |
| 30 | +``` |
| 31 | + |
| 32 | +Now simply run `make run` as usual to run it. |
| 33 | + |
| 34 | +This extended setup shows how you can have multiple instances of your Django (in a Kubernetes cluster, for example), and realtime notifications will work for any client connected to any of the servers. This example uses [broadcaster](https://github.com/encode/broadcaster) for subscription notifications which, in addition to `postgres`, supports `memory` (for a single node), `redis` and `kafka`. If your `django` is not using `postgres` and/or you are using one of the other options for caching or messaging, you might want to use one of those. It should Just Work if you follow the `broadcaster` docs for the connection string. |
0 commit comments