2024-01-30 20:32:43 +00:00
|
|
|
## Worker Relay
|
|
|
|
|
|
|
|
Worker relay is a Nostr relay built on `sqlite-wasm`
|
|
|
|
|
|
|
|
`WorkerRelayInterface` is the class which accepts the URL of the worker script
|
|
|
|
|
|
|
|
`sqlite-wasm` uses OFPS in order to persist the database.
|
|
|
|
|
2024-03-04 13:09:09 +00:00
|
|
|
### Example
|
2024-01-30 20:38:48 +00:00
|
|
|
|
2024-01-30 20:32:43 +00:00
|
|
|
```typescript
|
2024-03-04 13:09:09 +00:00
|
|
|
import { WorkerRelayInterface } from "@snort/worker-relay";
|
2024-01-30 20:32:43 +00:00
|
|
|
|
2024-03-04 15:25:15 +00:00
|
|
|
// when using Vite import the worker script directly (for production)
|
2024-03-07 14:12:50 +00:00
|
|
|
import WorkerVite from "@snort/worker-relay/src/worker?worker";
|
2024-01-30 20:38:48 +00:00
|
|
|
|
2024-03-04 15:25:15 +00:00
|
|
|
// in dev mode import esm module, i have no idea why it has to work like this
|
2024-03-07 14:12:50 +00:00
|
|
|
const workerScript = import.meta.env.DEV
|
|
|
|
? new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url)
|
|
|
|
: new WorkerVite();
|
2024-03-04 13:09:09 +00:00
|
|
|
|
2024-03-04 15:25:15 +00:00
|
|
|
const workerRelay = new WorkerRelayInterface(workerScript);
|
2024-01-30 20:32:43 +00:00
|
|
|
|
|
|
|
// load sqlite database and run migrations
|
2024-03-04 15:25:15 +00:00
|
|
|
await workerRelay.init("my-relay.db");
|
2024-01-30 20:32:43 +00:00
|
|
|
|
|
|
|
// Query worker relay with regular nostr REQ command
|
2024-03-04 15:25:15 +00:00
|
|
|
const results = await workerRelay.query(["REQ", "1", { kinds: [1], limit: 10 }]);
|
2024-01-30 20:32:43 +00:00
|
|
|
|
|
|
|
// publish a new event to the relay
|
|
|
|
const myEvent = {
|
2024-03-07 14:12:50 +00:00
|
|
|
kind: 1,
|
|
|
|
created_at: Math.floor(new Date().getTime() / 1000),
|
|
|
|
content: "test",
|
|
|
|
tags: [],
|
2024-01-30 20:32:43 +00:00
|
|
|
};
|
2024-03-04 15:25:15 +00:00
|
|
|
if (await workerRelay.event(myEvent)) {
|
2024-03-07 14:12:50 +00:00
|
|
|
console.log("Success");
|
2024-01-30 20:32:43 +00:00
|
|
|
}
|
2024-01-30 20:38:48 +00:00
|
|
|
```
|