Socket
WebSocket wrapper for Nostr relay connections with status tracking, queuing, and authentication support. Not intended to be used directly, instead access sockets through the Pool
interface.
Enums
SocketStatus
Connection status values:
Open
- Socket is connected and readyOpening
- Socket is connectingClosing
- Socket is closingClosed
- Socket is closedError
- Socket encountered an error
SocketEvent
Event types emitted by the socket:
Error
- Socket error occurredStatus
- Status changedSend
- Message sent to relaySending
- Message queued for sendingReceive
- Message received from relayReceiving
- Message queued for processing
Classes
Socket
WebSocket connection to a Nostr relay with queuing and authentication.
Properties:
url
- Relay URLstatus
- Current socket statusauth
- Authentication state
Methods:
open()
- Opens the WebSocket connectionattemptToOpen()
- Opens connection if not already openclose()
- Closes the connectioncleanup()
- Closes connection and removes all listenerssend(message)
- Queues a message to send
Example
typescript
import {Socket, SocketEvent, SocketStatus} from "@welshman/net"
const socket = new Socket("wss://relay.example.com")
socket.on(SocketEvent.Status, (status, url) => {
console.log(`Socket ${url} status: ${status}`)
})
socket.on(SocketEvent.Receive, (message, url) => {
console.log("Received:", message)
})
socket.open()
socket.send(["REQ", "sub-id", {kinds: [1], limit: 10}])
socket.cleanup()