1
1
Nostr Websocket Utils
2
2
3
- [ ![ npm version] ( https://img.shields.io/npm/v/@humanjavaenterprises/ nostr-websocket-utils.svg )] ( https://www.npmjs.com/package/@humanjavaenterprises /nostr-websocket-utils )
4
- [ ![ License] ( https://img.shields.io/npm/l/@humanjavaenterprises/ nostr-websocket-utils.svg )] ( https://github.com/HumanjavaEnterprises/nostr-websocket-utils/blob/main/LICENSE )
3
+ [ ![ npm version] ( https://img.shields.io/npm/v/nostr-websocket-utils.svg )] ( https://www.npmjs.com/package/nostr-websocket-utils )
4
+ [ ![ License] ( https://img.shields.io/npm/l/nostr-websocket-utils.svg )] ( https://github.com/HumanjavaEnterprises/nostr-websocket-utils/blob/main/LICENSE )
5
5
[ ![ Build Status] ( https://github.com/HumanjavaEnterprises/nostr-websocket-utils/workflows/CI/badge.svg )] ( https://github.com/HumanjavaEnterprises/nostr-websocket-utils/actions )
6
6
[ ![ TypeScript] ( https://img.shields.io/badge/TypeScript-Ready-blue.svg )] ( https://www.typescriptlang.org )
7
7
[ ![ code style: prettier] ( https://img.shields.io/badge/code_style-prettier-ff69b4.svg )] ( https://github.com/prettier/prettier )
@@ -22,15 +22,15 @@ A TypeScript library providing WebSocket utilities for Nostr applications, with
22
22
## Installation
23
23
24
24
``` bash
25
- npm install @humanjavaenterprises/ nostr-websocket-utils
25
+ npm install nostr-websocket-utils
26
26
```
27
27
28
- ## Breaking Changes in v0.2.0
28
+ ## Breaking Changes in v0.2.1
29
29
30
- - Introduced required handlers pattern for better type safety
31
- - Removed individual event handler properties (onMessage, onError, onClose)
32
- - Message handler is now required in server options
33
- - Client updated to match server interface
30
+ - Added UUID support for message tracking and correlation
31
+ - Fixed WebSocket mock implementation in tests
32
+ - Improved TypeScript type safety across the codebase
33
+ - Enhanced error handling for WebSocket connections
34
34
35
35
## Usage
36
36
@@ -39,7 +39,7 @@ npm install @humanjavaenterprises/nostr-websocket-utils
39
39
``` typescript
40
40
import express from ' express' ;
41
41
import { createServer } from ' http' ;
42
- import { NostrWSServer } from ' @humanjavaenterprises/ nostr-websocket-utils' ;
42
+ import { NostrWSServer } from ' nostr-websocket-utils' ;
43
43
import winston from ' winston' ;
44
44
45
45
const app = express ();
@@ -89,10 +89,53 @@ const wss = new NostrWSServer(server, {
89
89
server .listen (3000 );
90
90
```
91
91
92
+ ### Server Example with UUID
93
+
94
+ ``` typescript
95
+ import express from ' express' ;
96
+ import { createServer } from ' http' ;
97
+ import { NostrWSServer } from ' nostr-websocket-utils' ;
98
+ import winston from ' winston' ;
99
+
100
+ const app = express ();
101
+ const server = createServer (app );
102
+
103
+ // Create a logger
104
+ const logger = winston .createLogger ({
105
+ level: ' info' ,
106
+ format: winston .format .json (),
107
+ transports: [new winston .transports .Console ()]
108
+ });
109
+
110
+ // Initialize the WebSocket server with UUID support
111
+ const wsServer = new NostrWSServer ({
112
+ server ,
113
+ logger ,
114
+ messageHandler : async (message , client ) => {
115
+ // message.uuid will contain a unique identifier for the message
116
+ logger .info (` Received message with UUID: ${message .uuid } ` );
117
+
118
+ // Handle the message based on type
119
+ switch (message .type ) {
120
+ case ' subscribe' :
121
+ // Handle subscription
122
+ break ;
123
+ case ' event' :
124
+ // Handle event
125
+ break ;
126
+ default :
127
+ // Handle unknown message type
128
+ }
129
+ }
130
+ });
131
+
132
+ server .listen (3000 );
133
+ ```
134
+
92
135
### Client Example
93
136
94
137
``` typescript
95
- import { NostrWSClient } from ' @humanjavaenterprises/ nostr-websocket-utils' ;
138
+ import { NostrWSClient } from ' nostr-websocket-utils' ;
96
139
import winston from ' winston' ;
97
140
98
141
// Create a logger
@@ -130,6 +173,30 @@ client.on('connect', () => {
130
173
client .connect ();
131
174
```
132
175
176
+ ### Client Example with UUID
177
+
178
+ ``` typescript
179
+ import { NostrWSClient } from ' nostr-websocket-utils' ;
180
+
181
+ const client = new NostrWSClient (' ws://localhost:3000' , {
182
+ logger: console
183
+ });
184
+
185
+ client .on (' message' , (message ) => {
186
+ // Access the UUID of the received message
187
+ console .log (` Received message with UUID: ${message .uuid } ` );
188
+ });
189
+
190
+ // Connect to the server
191
+ client .connect ();
192
+
193
+ // Send a message (UUID will be automatically generated)
194
+ client .send ({
195
+ type: ' event' ,
196
+ data: { content: ' Hello, World!' }
197
+ });
198
+ ```
199
+
133
200
## Interface Reference
134
201
135
202
### NostrWSOptions
0 commit comments