|
1 |
| -# Nostr Websocket Utils |
| 1 | +# @humanjavaenterprises/nostr-websocket-utils |
2 | 2 |
|
3 |
| -[](https://www.npmjs.com/package/nostr-websocket-utils) |
4 |
| -[](https://github.com/HumanjavaEnterprises/nostr-websocket-utils/blob/main/LICENSE) |
| 3 | +[](https://www.npmjs.com/package/@humanjavaenterprises/nostr-websocket-utils) |
| 4 | +[](https://github.com/HumanjavaEnterprises/nostr-websocket-utils/blob/main/LICENSE) |
5 | 5 | [](https://github.com/HumanjavaEnterprises/nostr-websocket-utils/actions)
|
| 6 | +[](https://humanjavaenterprises.github.io/nostr-websocket-utils/) |
6 | 7 | [](https://www.typescriptlang.org)
|
7 | 8 | [](https://github.com/prettier/prettier)
|
8 | 9 |
|
9 |
| -A TypeScript library providing WebSocket utilities for Nostr applications, focusing on robust connection handling, automatic reconnection, and channel-based messaging. This library has been streamlined to remove any DOM-related code, enhancing performance and maintainability. |
| 10 | +A TypeScript library for building Nostr protocol WebSocket clients and servers. |
10 | 11 |
|
11 | 12 | ## Features
|
12 | 13 |
|
13 |
| -- 🔄 Automatic reconnection with configurable attempts |
14 |
| -- 💓 Heartbeat monitoring with configurable intervals |
15 |
| -- 📨 Message queueing during disconnections |
16 |
| -- 📢 Channel-based broadcasting with filtered subscriptions |
17 |
| -- 🔒 Type-safe message handling with required handlers |
18 |
| -- 📝 Built-in logging with Winston integration |
19 |
| -- 🛡️ Comprehensive error handling and validation |
20 |
| -- 🧪 100% test coverage with Jest |
21 |
| -- 📦 Zero DOM dependencies |
22 |
| -- 🔍 Full TypeScript type safety |
| 14 | +- 🚀 Full Nostr protocol support |
| 15 | +- 🔒 Secure WebSocket connections |
| 16 | +- ♥️ Heartbeat mechanism for connection health |
| 17 | +- 🔄 Automatic reconnection handling |
| 18 | +- 📝 Comprehensive logging |
| 19 | +- 🎯 Type-safe message handling |
| 20 | +- 📦 Easy to use API |
23 | 21 |
|
24 | 22 | ## Installation
|
25 | 23 |
|
26 | 24 | ```bash
|
27 |
| -npm install nostr-websocket-utils |
| 25 | +npm install @humanjavaenterprises/nostr-websocket-utils |
28 | 26 | ```
|
29 | 27 |
|
30 |
| -## Breaking Changes in v0.2.4 |
| 28 | +## Quick Start |
31 | 29 |
|
32 |
| -- 🆕 Added dedicated Nostr WebSocket server implementation |
33 |
| -- 📝 Enhanced TypeScript type definitions for Nostr messages |
34 |
| -- 🔄 Improved message handling with strict type checking |
35 |
| -- 🧪 Added comprehensive test suite for Nostr server |
36 |
| -- 🛡️ Strengthened type safety with `unknown` types |
37 |
| -- 🔌 Added support for Nostr EVENT and REQ messages |
| 30 | +### Creating a Nostr WebSocket Client |
38 | 31 |
|
39 |
| -## Usage |
| 32 | +```typescript |
| 33 | +import { NostrWSClient } from '@humanjavaenterprises/nostr-websocket-utils'; |
| 34 | + |
| 35 | +const client = new NostrWSClient('wss://relay.example.com', { |
| 36 | + logger: console, |
| 37 | + heartbeatInterval: 30000, |
| 38 | + handlers: { |
| 39 | + message: async (msg) => console.log('Received:', msg), |
| 40 | + error: (err) => console.error('Error:', err), |
| 41 | + close: () => console.log('Connection closed') |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +await client.connect(); |
| 46 | +``` |
40 | 47 |
|
41 |
| -### Nostr Server Example |
| 48 | +### Creating a Nostr WebSocket Server |
42 | 49 |
|
43 | 50 | ```typescript
|
44 |
| -import { NostrWSServer, createWSServer } from 'nostr-websocket-utils'; |
45 |
| -import { NostrWSMessageType, NostrWSEvent } from 'nostr-websocket-utils/types/nostr'; |
| 51 | +import { createNostrServer } from '@humanjavaenterprises/nostr-websocket-utils'; |
46 | 52 |
|
47 |
| -// Create Nostr WebSocket server |
48 |
| -const server = createWSServer({ |
49 |
| - port: 8080, |
50 |
| - heartbeatInterval: 30000, // Optional: 30 seconds |
| 53 | +const server = await createNostrServer(8080, { |
| 54 | + logger: console, |
| 55 | + heartbeatInterval: 30000, |
51 | 56 | handlers: {
|
52 |
| - // Required: Handle incoming messages |
53 |
| - message: async (socket, message) => { |
54 |
| - switch (message[0]) { |
55 |
| - case NostrWSMessageType.EVENT: |
56 |
| - const event = message[1] as NostrWSEvent; |
57 |
| - // Handle Nostr event |
58 |
| - break; |
59 |
| - case NostrWSMessageType.REQ: |
60 |
| - const [_type, subscriptionId, filter] = message; |
61 |
| - // Handle subscription request |
62 |
| - break; |
63 |
| - } |
64 |
| - }, |
65 |
| - // Optional: Handle errors |
66 |
| - error: (socket, error) => { |
67 |
| - console.error('WebSocket error:', error); |
68 |
| - }, |
69 |
| - // Optional: Handle client disconnection |
70 |
| - close: (socket) => { |
71 |
| - console.info('Client disconnected'); |
| 57 | + message: async (ws, msg) => { |
| 58 | + console.log('Received message:', msg); |
| 59 | + // Handle the message |
72 | 60 | }
|
73 | 61 | }
|
74 | 62 | });
|
| 63 | +``` |
| 64 | + |
| 65 | +## Documentation |
| 66 | + |
| 67 | +Full API documentation is available in the [docs](./docs) directory. The documentation includes: |
75 | 68 |
|
76 |
| -// Start listening |
77 |
| -server.listen(); |
| 69 | +- Detailed API reference |
| 70 | +- Type definitions |
| 71 | +- Examples and usage patterns |
| 72 | +- Best practices |
| 73 | + |
| 74 | +To generate the documentation locally: |
| 75 | + |
| 76 | +```bash |
| 77 | +npm run docs |
78 | 78 | ```
|
79 | 79 |
|
80 |
| -### Types |
| 80 | +This will create a `docs` directory with the full API documentation. |
| 81 | + |
| 82 | +## Examples |
| 83 | + |
| 84 | +### Subscribe to a Channel |
81 | 85 |
|
82 | 86 | ```typescript
|
83 |
| -// Nostr Event Type |
84 |
| -interface NostrWSEvent { |
85 |
| - id: string; |
86 |
| - pubkey: string; |
87 |
| - created_at: number; |
88 |
| - kind: number; |
89 |
| - tags: string[][]; |
90 |
| - content: string; |
91 |
| - sig: string; |
92 |
| -} |
93 |
| - |
94 |
| -// Nostr Filter Type |
95 |
| -interface NostrWSFilter { |
96 |
| - ids?: string[]; |
97 |
| - authors?: string[]; |
98 |
| - kinds?: number[]; |
99 |
| - '#e'?: string[]; |
100 |
| - '#p'?: string[]; |
101 |
| - since?: number; |
102 |
| - until?: number; |
103 |
| - limit?: number; |
104 |
| -} |
105 |
| - |
106 |
| -// Message Types |
107 |
| -enum NostrWSMessageType { |
108 |
| - EVENT = 'EVENT', |
109 |
| - REQ = 'REQ', |
110 |
| - CLOSE = 'CLOSE', |
111 |
| - NOTICE = 'NOTICE', |
112 |
| - AUTH = 'AUTH', |
113 |
| - EOSE = 'EOSE' |
114 |
| -} |
| 87 | +client.subscribe('my-channel', { |
| 88 | + filter: { |
| 89 | + authors: ['pubkey1', 'pubkey2'], |
| 90 | + kinds: [1] |
| 91 | + } |
| 92 | +}); |
115 | 93 | ```
|
116 | 94 |
|
117 |
| -## Advanced Configuration |
118 |
| - |
119 |
| -### Server Options |
| 95 | +### Broadcast a Message |
120 | 96 |
|
121 | 97 | ```typescript
|
122 |
| -interface NostrWSServerOptions { |
123 |
| - port: number; |
124 |
| - heartbeatInterval?: number; |
125 |
| - maxPayloadSize?: number; |
126 |
| - cors?: { |
127 |
| - origin?: string | string[]; |
128 |
| - methods?: string[]; |
129 |
| - }; |
130 |
| - handlers: { |
131 |
| - message: MessageHandler; |
132 |
| - error?: ErrorHandler; |
133 |
| - close?: CloseHandler; |
134 |
| - }; |
135 |
| -} |
| 98 | +server.broadcast({ |
| 99 | + type: 'EVENT', |
| 100 | + data: { |
| 101 | + content: 'Hello everyone!', |
| 102 | + kind: 1 |
| 103 | + } |
| 104 | +}); |
136 | 105 | ```
|
137 | 106 |
|
138 | 107 | ## Contributing
|
139 | 108 |
|
140 |
| -Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change. |
| 109 | +Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests. |
141 | 110 |
|
142 | 111 | ## License
|
143 | 112 |
|
144 | 113 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
| 114 | + |
| 115 | +## Related Projects |
| 116 | + |
| 117 | +- [nostr-protocol](https://github.com/nostr-protocol/nostr) |
| 118 | +- [nostr-tools](https://github.com/nbd-wtf/nostr-tools) |
| 119 | + |
| 120 | +## Support |
| 121 | + |
| 122 | +If you have any questions or need help, please: |
| 123 | + |
| 124 | +1. Check the [documentation](./docs) |
| 125 | +2. Open an [issue](https://github.com/HumanjavaEnterprises/nostr-websocket-utils/issues) |
| 126 | +3. Join our [Discord community](https://discord.gg/your-discord) |
0 commit comments