Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Remove references to MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBl69 committed Aug 7, 2024
1 parent 02e7e5d commit b8a18db
Show file tree
Hide file tree
Showing 19 changed files with 21 additions and 2,601 deletions.
229 changes: 18 additions & 211 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,26 @@ ONLY*) to this table.

### Events

You can also use SQL extensions to create events that will then appear in MQTT.
To do so, follow this example:

```sql
CREATE OR REPLACE FUNCTION notify_event()
RETURNS trigger AS $$
BEGIN
PERFORM pg_notify(
'inbox_event',
(SELECT jsonb_build_object('topic', NEW.type, 'payload', to_jsonb(NEW))::text));
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER notify_event
AFTER INSERT ON inbox_events
FOR EACH ROW
EXECUTE PROCEDURE notify_event();
You can get SSE events by querying the publisher.

See Rust comment doc for SSE details:

```rust
/// Handles a request to `/sse`.
///
/// Takes subscription data as query parameters.
///
/// If a field is left empty, you will be subscribed to all.
///
/// Example of connection paths:
///
/// - `/sse?markets=1&event_types=Chat`: subscribe to chat events on market 1
/// - `/sse?markets=1`: subscribe to all events on market 1
/// - `/sse?event_types=State`: subscribe to all State events
/// - `/sse`: subscribe to all events
/// - `/sse?markets=1&markets=2&event_types=Chat&event_types=Swap`: subscribe to Chat and Swap events on markets 1 and 2
```

This will emit an MQTT event with the topic as your event type for all your
contract's events.

## Terraform

You can deploy this repo on GCP using Terraform.
Expand Down Expand Up @@ -233,196 +230,6 @@ If you are using a branch that by default requires authentication for PostgREST,
like the `emojicoin-dot-fun` branch, you'll need to select
`Allow unauthenticated invocations` under `Cloud Run > postgrest > security`.
### 8. Issue a TLS certificate (optional)
Note that for local development where `inbox` is running through Docker compose,
browsers like Chrome should be able to connect to the `mqtt` endpoint over an
unsecured `ws` localhost connection. However, when connecting to an endpoint
from a production `mqtt` server, the connection will need to be over a secure
`wss` connection.
Hence for a production Terraform deployment, you'll need to issue a TLS
certificate to the `mqtt` instance:
1. Get the public IP of the `mqtt` VM:
```sh
gcloud compute instances list
```

1. Create a new custom DNS record for your preferred domain:

| Host | Type | Priority | Data |
| ---- | ---- | -------- | -------------------- |
| `@` | `A` | N/A | `<MQTT_EXTERNAL_IP>` |

1. Verify the domain has resolved to the IP address (there may be a delay):

```sh
npx wscat -c ws://<YOUR_DOMAIN>:21884
```

1. Get your IP address:

```sh
MY_IP=$(curl --silent http://checkip.amazonaws.com)
```

1. Create a temporary firewall rule that will allow you to SSH into the `mqtt`
VM:

```sh
gcloud compute firewall-rules create set-cert \
--allow tcp:22 \
--direction INGRESS \
--network sql-network \
--priority 0 \
--source-ranges $MY_IP/32
```

1. Create a temporary firewall rule that will allow `certbot` to connect:

```sh
gcloud compute firewall-rules create certbot \
--allow tcp:80 \
--direction INGRESS \
--network sql-network \
--priority 0 \
--source-ranges 0.0.0.0/0
```

1. Optionally verify you can connect via

```sh
curl -I http://<YOUR_DOMAIN>:80
```

1. SSH into the `mqtt` VM:

```sh
gcloud compute ssh mqtt
```

1. Run:

```sh
docker ps
```

1. Enter the container with an interactive `sh` session:

```sh
docker exec -it <CONTAINER_ID> sh
```

1. Activate superuser:

```sh
su
```

1. Install packages:

```sh
apt update
apt install certbot vim
```

1. Try a dry run:

```sh
certbot certonly --standalone --dry-run
```

1. If if succeeds:

```sh
certbot certonly --standalone
```

1. Copy files:

```sh
cp /etc/letsencrypt/live/<YOUR_DOMAIN>/chain.pem /cafile
cp /etc/letsencrypt/live/<YOUR_DOMAIN>/cert.pem /certfile
cp /etc/letsencrypt/live/<YOUR_DOMAIN>/privkey.pem /keyfile
```

1. Vim into the `mosquitto` config file:

```sh
vim /mosquitto/config/mosquitto.conf
```

1. Under the `listener 21884` block, add TLS file lookup options and
`required false` so that your config looks like:

```sh
per_listener_settings true
listener 21883
protocol mqtt
allow_anonymous true
password_file /password_file
acl_file /acl_file
listener 21884
protocol websockets
allow_anonymous true
password_file /password_file
acl_file /acl_file
# New contents below
certfile /certfile
cafile /cafile
keyfile /keyfile
require_certificate false
```

1. Update file privileges:

```sh
chmod 755 certfile
chmod 755 cafile
chmod 755 keyfile
chown mosquitto:mosquitto certfile
chown mosquitto:mosquitto cafile
chown mosquitto:mosquitto keyfile
```

1. Exit the `su` prompt, then the container via `exit`.

1. Get the container via `docker ps`.

1. Restart the container via `docker restart <CONTAINER_ID>`.

1. Run `docker ps` several times to verify the container is up and running.

1. `exit` out of the VM.

1. Verify you can connect to `wss`:

```sh
npx wscat -c wss://<YOUR_DOMAIN>:21884
```

1. Delete the temporary firewall rules:

```sh
gcloud compute firewall-rules delete set-cert
gcloud compute firewall-rules delete certbot
```

1. Pro tip: if you perform a step incorrectly, you can always start with a fresh
`mqtt` instance:

```sh
terraform destroy -target module.mqtt -var-file variables.tfvars
```

```sh
terraform apply -target module.mqtt -var-file variables.tfvars
```

Note that GCP issues ephemeral IP addresses for VMs, which means they only
persist for the lifetime of the resource. So if you need to start over then the
corresponding public IP address will probably change.
Expand Down
6 changes: 3 additions & 3 deletions cfg/pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ repos:
- id: fmt
args:
- '--manifest-path'
- mqtt-publisher/Cargo.toml
- publisher/Cargo.toml
- '--'
- id: cargo-check
args:
- '--manifest-path'
- mqtt-publisher/Cargo.toml
- publisher/Cargo.toml
- '--'
- id: clippy
args:
- '--manifest-path'
- mqtt-publisher/Cargo.toml
- publisher/Cargo.toml
- '--'
- repo: 'https://github.com/sqlfluff/sqlfluff'
rev: 3.0.6
Expand Down
4 changes: 0 additions & 4 deletions example.env
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,3 @@ STARTING_VERSION="0"
# A hard limit to the number of rows PostgREST will fetch from a view, table, or stored procedure.
# Limits payload size for accidental or malicious requests.
POSTGREST_MAX_ROWS="500"

# A password that will be used to publish to MQTT. It is recommended to be a
# strong password to avoid brute force attacks.
MOSQUITTO_PASSWORD="my_secure_password"
1 change: 0 additions & 1 deletion mqtt-publisher/.gitignore

This file was deleted.

Loading

0 comments on commit b8a18db

Please sign in to comment.