💧 hydrogen.js - relays and hooks
hydrogen.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.
let sub = hydrogen.req({filters:[{kinds:[0], authors:[pk]}], onevent:(event, from)=>{}})
let signed = await hydrogen.sign({event:{kind:1, created_at, tags:[], content:"hi"}})
let results = await hydrogen.pub(signed, hydrogen.relays()) // [{relay, ok, msg}]
That subscribes, signs with whatever signer is present, and publishes to the page's relays.
Attributes
| attribute | description |
|---|---|
nhy-relay |
Space-separated relay URLs, inherited from the closest ancestor. Falls back to window.nhyCfg.relays |
window.nhyCfg = {relays, timeout}: default relays, and publish timeout in ms (default 8000).
Events (the family hooks)
| event | on | cancel to | detail |
|---|---|---|---|
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 |
nhy:event |
cfg.elt or document |
skip cfg.onevent |
cfg, event, from (relay URL, or a cache name), fields (escaped, for templates) |
nhy:message |
document | skip hydrogen's own handling | url, relay, msg: every raw relay message, before anything else |
Signers (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.
API
| function | returns |
|---|---|
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 |
hydrogen.sign(cfg) |
the signed cfg.event, after nhy:config. The default signer is window.nostr |
hydrogen.pub(event, relays) |
[{relay, ok, msg}] per relay |
hydrogen.relays(elt?) |
relay URLs for an element |
hydrogen.relay(url) |
the pooled connection: send(msg), subs, oks, ws |
hydrogen.esc(text) |
escaped HTML |