# Topics

Live, transient publish/subscribe over HTTP and WebSockets with wildcard patterns and scoped subscribe credentials.

**Topics** provide low-latency, in-memory publish/subscribe scoped to your organization. Messages are strictly **transient** and are never persisted to disk or a database.

## Publishing Messages

Publish raw bytes over HTTP through the control API, MCP, or a Worker/Sandbox `topic` binding:

:::tabs
::tab skopik
```bash
skopik publish_to_topic --topic chat/lobby --body '{"user":"ada","text":"hi"}'
```
::tab curl
```bash
curl -X POST https://api.platform.skopik.com/v1/topics/publish/chat/lobby \
  -H "Authorization: Bearer $SKOPIK_API_KEY" \
  -H "content-type: application/json" \
  -d '{"user": "ada", "text": "hi"}'
```
::tab MCP
```json
{
  "tool": "publish_to_topic",
  "arguments": {
    "topic": "chat/lobby",
    "body": "{\"user\":\"ada\",\"text\":\"hi\"}"
  }
}
```
:::

## Subscribing Over WebSocket

Clients connect to the public Topics endpoint:

```text
wss://topics.platform.skopik.com/v1/subscribe
```

- **Wildcard patterns:** Use `*` to match one path segment (e.g. `chat/*`) or `**` to match across segments (e.g. `chat/**`).
- **Browser clients without API keys:** Mint a short-lived, pattern-scoped subscribe credential via `POST /v1/topics/credentials` (or through a Worker's Topic binding at `POST $SKOPIK_BINDINGS_URL/topics/{binding}/credentials`) with `{"patterns": ["chat/lobby"], "ttl_seconds": 300}`.

```js
// Inside a Worker with a Topic binding "EVENTS": { "topic": "chat/**" }
const res = await fetch(`${process.env.SKOPIK_BINDINGS_URL}/topics/EVENTS/credentials`, {
  method: 'POST',
  headers: { 'mh-bindings': '1', 'content-type': 'application/json' },
  body: JSON.stringify({ patterns: ['chat/room-42'], ttl_seconds: 300 }),
})
const { token } = await res.json()
```

> [!NOTE]
> A Topic binding can only publish to topics or mint credentials for patterns covered by the binding's declared pattern. Requests outside the pattern are refused with `403 pattern_not_allowed`.
