1# 💧 hydrogen.js - relays and hooks23hydrogen.js is the element everything else is made of: a miniature relay pool with the family's shared hooks. It keeps one connection per relay, reconnects, dedupes events, and runs every subscription, publish and signature through three DOM events that the other libraries listen to. It has no attributes of its own besides `nhy-relay`, and runs in Deno too.45```js6let sub = hydrogen.req({filters:[{kinds:[0], authors:[pk]}], onevent:(event, from)=>{}})7let signed = await hydrogen.sign({event:{kind:1, created_at, tags:[], content:"hi"}})8let results = await hydrogen.pub(signed, hydrogen.relays()) // [{relay, ok, msg}]9```1011That subscribes, signs with whatever signer is present, and publishes to the page's relays.1213## Attributes1415| attribute | description |16|---|---|17| `nhy-relay` | Space-separated relay URLs, inherited from the closest ancestor. Falls back to `window.nhyCfg.relays` |1819`window.nhyCfg = {relays, timeout}`: default relays, and publish timeout in ms (default 8000).2021## Events (the family hooks)2223| event | on | cancel to | detail |24|---|---|---|---|25| `nhy:config` | `cfg.elt` or document | skip the request or signature | `cfg`: live. Subscriptions carry `filters`, `relays`, `verify`, `map`, `onevent`. Signatures carry `event` and `sign` |26| `nhy:event` | `cfg.elt` or document | skip `cfg.onevent` | `cfg`, `event`, `from` (relay URL, or a cache name), `fields` (escaped, for templates) |27| `nhy:message` | document | skip hydrogen's own handling | `url`, `relay`, `msg`: every raw relay message, before anything else |2829Signers (helium, chlorine, an extension shim) set `cfg.sign` in `nhy:config`. Caches (argon) answer subscriptions there and store from `nhy:event`. Formatters (neon) add fields in `nhy:event`. Relay AUTH (gold) works through `nhy:message`.3031## API3233| function | returns |34|---|---|35| `hydrogen.req(cfg)` | the live `cfg` with `close()`, or nothing if `nhy:config` was cancelled. `cfg.elt` scopes events and relays; `cfg.verify(event)` and `cfg.map(event)` run before `nhy:event` |36| `hydrogen.sign(cfg)` | the signed `cfg.event`, after `nhy:config`. The default signer is `window.nostr` |37| `hydrogen.pub(event, relays)` | `[{relay, ok, msg}]` per relay |38| `hydrogen.relays(elt?)` | relay URLs for an element |39| `hydrogen.relay(url)` | the pooled connection: `send(msg)`, `subs`, `oks`, `ws` |40| `hydrogen.esc(text)` | escaped HTML |