|
| 1 | +# FastAPI Postgres Pub/Sub Chat |
| 2 | + |
| 3 | +[](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-fastapi-postgres-pubsub-template%26template_owner%3DDefangSamples) |
| 4 | + |
| 5 | +This sample pairs FastAPI with PostgreSQL `LISTEN/NOTIFY` to demonstrate real-time updates between two application containers. A minimal chat UI sends messages with a REST request, and both FastAPI instances broadcast the new message over WebSockets after Postgres notifies them. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +1. Download [Defang CLI](https://github.com/DefangLabs/defang) |
| 10 | +2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account |
| 11 | +3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/) |
| 12 | + |
| 13 | +## Development |
| 14 | + |
| 15 | +To run the application locally, you can use the following command: |
| 16 | + |
| 17 | +```bash |
| 18 | +docker compose -f compose.dev.yaml up --build |
| 19 | +``` |
| 20 | + |
| 21 | +Once everything is running: |
| 22 | +- Visit [http://localhost:8000](http://localhost:8000) for the first FastAPI service. |
| 23 | +- Visit [http://localhost:8001](http://localhost:8001) for the second service. |
| 24 | +- Send a chat message in either window. Both pages should update immediately, proving Postgres `LISTEN/NOTIFY` fans the event across containers. |
| 25 | + |
| 26 | +Stop the stack with `Ctrl+C`, then run `docker compose -f compose.dev.yaml down`. |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +For this sample, you can rely on the defaults. Override them with environment variables if needed: |
| 31 | + |
| 32 | +> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you. |
| 33 | +
|
| 34 | +### `POSTGRES_PASSWORD` |
| 35 | +Database password (default `chat_password`). |
| 36 | +```bash |
| 37 | +defang config set POSTGRES_PASSWORD --random |
| 38 | +``` |
| 39 | + |
| 40 | +### `SSL_MODE` |
| 41 | + |
| 42 | +Postgres SSL mode (default `disable`, should set to `require` in production). |
| 43 | +```bash |
| 44 | +defang config set SSL_MODE=require |
| 45 | +``` |
| 46 | + |
| 47 | +## Deployment |
| 48 | + |
| 49 | +> [!NOTE] |
| 50 | +> Download [Defang CLI](https://github.com/DefangLabs/defang) |
| 51 | +
|
| 52 | +### Defang Playground |
| 53 | + |
| 54 | +Deploy your application to the Defang Playground by opening up your terminal and typing: |
| 55 | +```bash |
| 56 | +defang compose up |
| 57 | +``` |
| 58 | + |
| 59 | +### BYOC |
| 60 | + |
| 61 | +If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud). |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +Title: FastAPI Postgres Pub/Sub |
| 66 | + |
| 67 | +Short Description: FastAPI sample that stores messages in Postgres and streams them to two app instances via LISTEN/NOTIFY. |
| 68 | + |
| 69 | +Tags: FastAPI, PostgreSQL, WebSockets, PubSub |
| 70 | + |
| 71 | +Languages: Python, SQL |
0 commit comments