A TypeScript library for building Nostr protocol WebSocket clients and servers.
π’ Fully implemented π‘ Partially implemented π΄ Not implemented
NIP | Status | Description |
---|---|---|
01 | π’ | Basic protocol flow & WebSocket connections |
02 | π’ | Contact List and Petnames |
11 | π’ | Relay Information Document |
15 | π’ | End of Stored Events Notice |
16 | π’ | Event Treatment |
20 | π’ | Command Results |
42 | π’ | Authentication of clients to relays |
This package implements the Nostr WebSocket protocol with full support for the core NIPs that define WebSocket behavior. Hereβs how it works:
This implementation ensures compatibility with:
The package includes:
npm install nostr-websocket-utils
import { NostrWSClient } from 'nostr-websocket-utils';
const client = new NostrWSClient('wss://relay.example.com', {
logger: console,
heartbeatInterval: 30000,
handlers: {
message: async (msg) => console.log('Received:', msg),
error: (err) => console.error('Error:', err),
close: () => console.log('Connection closed')
}
});
await client.connect();
import { createNostrServer } from '@humanjavaenterprises/nostr-websocket-utils';
const server = await createNostrServer(8080, {
logger: console,
heartbeatInterval: 30000,
handlers: {
message: async (ws, msg) => {
console.log('Received message:', msg);
// Handle the message
}
}
});
This library now supports direct browser usage! You can use it in your client-side applications in two ways:
import { NostrWSClient } from 'nostr-websocket-utils';
const client = new NostrWSClient({
url: 'wss://relay.damus.io',
options: {
autoReconnect: true,
maxRetries: 3
}
});
client.onMessage((message) => {
console.log('Received:', message);
});
client.connect();
<script src="https://unpkg.com/nostr-websocket-utils/dist/browser/nostr-websocket-utils.min.js"></script>
<script>
const client = new NostrWebSocketUtils.NostrWSClient({
url: 'wss://relay.damus.io',
options: {
autoReconnect: true,
maxRetries: 3
}
});
client.onMessage((message) => {
console.log('Received:', message);
});
client.connect();
</script>
See the examples/browser.html
file for a complete example of browser usage.
This package uses:
Comprehensive API documentation is available in our documentation site. Hereβs what youβll find:
client.subscribe('my-channel', {
filter: {
authors: ['pubkey1', 'pubkey2'],
kinds: [1]
}
});
server.broadcast({
type: 'event',
data: {
content: 'Hello everyone!',
kind: 1
}
});
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
If you have any questions or need help, please: