Download the PHP package innis/nostr-client without Composer
On this page you can find all versions of the php package innis/nostr-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package nostr-client
innis/nostr-client
AMPHP-based async WebSocket client for Nostr protocol
A PHP client library for connecting to Nostr relays over WebSocket, subscribing to events, and publishing. Built with AMPHP for non-blocking concurrent relay connections and clean architecture principles.
Features
- Multi-relay connections - Connect to multiple relays concurrently
- AMPHP async - Non-blocking WebSocket I/O with fibers
- Subscription management - Subscribe with single or multiple filters, receive events via handler callbacks
- Event publishing - Publish signed events with OK response handling
- NIP-42 authentication - Automatic auth challenge handling with transparent publish and subscription retry, and subscriptions a relay narrowed (NIP-67
authhint) sent again once authenticated - Connection lifecycle - Automatic state tracking, health checks, reconnection, ping
- Keep-alive handling - WebSocket heartbeats and application-level ping responses
- PSR-3 logging - Standard logging interface throughout
- Clean Architecture - Strict layer separation with domain objects from
innis/nostr-core
Requirements
- PHP 8.4 or higher
innis/nostr-core- Core Nostr protocol entitiesamphp/amp^3.0 - Async runtimeamphp/websocket-client^2.0 - WebSocket clientpsr/log^3.0 - Logging interface
Installation
Quick Start
Connect and Subscribe
Publish Events
A relay accepting or rejecting an event (duplicate, rate-limited, blocked, …) is an anticipated outcome carried in the PublishResult. So is a relay that cannot answer: a publish to a relay the client is not connected to, or whose connection drops before its OK, resolves not accepted with the message disconnected, and one the relay never answers within ConnectionConfig's publishTimeoutMs (default 8000) resolves with timeout. These messages are the client's own and carry no reason prefix, so they are never mistaken for a relay's reason; they are the cases of RelayUnavailability. A publish future never errors for a relay that is down.
Health Checking
healthCheck() pings every currently connected relay over its existing connection and reports whether each is still reachable. To probe a relay you are not connected to, use the standalone health checker below.
Multiple Filters Per Subscription
Subscription Ids
The client assigns a subscription id when the request names none. A request may name its own, except keepalive: the client sends ["CLOSE", "keepalive"] as its keep-alive, so building a SubscriptionRequest with that id throws an InvalidArgumentException.
Connection Configuration
connect() accepts an optional ConnectionConfig. It controls the connection timeout, request headers, user agent, and auto-reconnect behaviour. It is immutable; construct it with named arguments, defaulting anything you do not set.
connect() returns a ConnectResult rather than throwing: a relay that cannot be reached is an outcome, reported as failed to connect, and every other relay's connection is unaffected.
Auto-reconnect is enabled by default. A dropped connection retries on jittered exponential backoff between reconnectInitialDelayMs and reconnectMaxDelayMs. reconnectMaxAttempts of 0 means unlimited retries; a positive value bounds them.
Connection Management
getConnectionStatus() returns a ConnectionState: DISCONNECTED, CONNECTED, DISCONNECTING, or FAILED.
ping() returns a HealthCheckResult, unhealthy with disconnected when the relay is not connected. Subscribing to a relay that is not connected returns the subscription id and closes it at once through the handler's handleClosed() with disconnected; a subscription open when the connection drops is closed the same way. Unsubscribing from a relay that is not connected does nothing.
Reconnection Listener
Register a listener to re-establish per-connection state (re-subscribe, re-authenticate) after a dropped connection is restored. The listener fires only on a successful reconnect.
Awaiting Publishes
publishEvent() returns a Future<PublishResult> as soon as the event has been sent — await each future for that publish's individual outcome. To instead block until every in-flight publish for a relay has been acknowledged (including any parked on a NIP-42 auth challenge) without inspecting each result, drain them with an optional timeout in seconds.
NIP-42 Authentication
Register an auth handler to sign relay challenges. When a publish is refused with OK false or a subscription with CLOSED, prefixed auth-required:, the client answers the relay's challenge and parks the work. Once the relay accepts the AUTH with OK true, the event is resent and the REQ re-issued on the same subscription id, so neither the publish's future nor the subscription's handler sees the refusal. If the relay rejects the AUTH, the handler declines by returning null, or no verdict arrives within ConnectionConfig's authTimeoutMs (default 60000), the parked publish resolves as rejected and the parked subscription is closed, with a reason beginning auth-required:. Without a registered handler the relay's refusal is returned as is.
A relay may instead serve only what a guest may read and end the stored events with NIP-67's "auth" hint (["EOSE", <id>, ["auth"]]). That subscription stays open and is not parked. Once the relay accepts the client's AUTH, or at once if it already has, the client sends its REQ again on the same id, once, so the relay serves it at the authenticated scope. The handler then receives a second handleEose, and may receive events it already had. A repeated EOSE for one REQ is ignored.
Standalone Health Checker
Check relay health without an active connection:
See examples/ for complete working examples.
Error Handling
Anticipated outcomes (a well-formed operation whose answer is "no") are returned as typed values (?T or a *Failure); faults are thrown. For a relay client, a relay that cannot be reached, drops the connection, or never answers is an anticipated outcome, returned as that relay's result: a ConnectResult, a PublishResult, a HealthCheckResult, or a subscription closed with disconnected. It is never thrown, so one relay being down never costs the answers of the others. See ADR-0015.
What remains thrown is a broken client: a misused API, a broken invariant, a failure of the client's own machinery. nostr-client's faults are ClientException (abstract) extending NostrException, with ConnectionException (final) extending ClientException. Catch NostrException to handle faults from any nostr-* library. See ADR-0002 for how faults are rooted.
Retry logic belongs in your application layer where you have full business context.
Architecture
This package follows Clean Architecture principles:
Testing
Licence
MIT License. See LICENSE file for details.
All versions of nostr-client with dependencies
amphp/amp Version ^3.1.3
amphp/websocket-client Version ^2.0.2
innis/nostr-core Version ^0.11
psr/log Version ^3.0.2