r2-notify-client 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +303 -0
- package/dist/index.cjs +407 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +78 -0
- package/dist/index.d.ts +78 -0
- package/dist/index.js +393 -0
- package/dist/index.js.map +1 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sherantha Perera
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
# r2-notify-client
|
|
2
|
+
|
|
3
|
+
Framework-agnostic WebSocket notification client for r2-notify-server. Consumes real-time events and emits notification actions.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install r2-notify-client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- ð **WebSocket-based real-time notifications**
|
|
14
|
+
- ð **Automatic reconnection** with configurable delay
|
|
15
|
+
- ðŊ **Type-safe event handling** with TypeScript
|
|
16
|
+
- ðŠķ **Framework-agnostic** - works with React, Vue, vanilla JS, or any other framework
|
|
17
|
+
- ðĶ **Lightweight** with minimal dependencies
|
|
18
|
+
- ð **Event-driven architecture** using EventEmitter3
|
|
19
|
+
- ð **Token-based authentication** support (Road Map)
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { R2NotifyClient } from 'r2-notify-client';
|
|
25
|
+
|
|
26
|
+
const client = new R2NotifyClient({
|
|
27
|
+
url: 'wss://your-websocket-server.com',
|
|
28
|
+
clientId: 'unique-user-id',
|
|
29
|
+
token: 'optional-auth-token',
|
|
30
|
+
reconnect: true,
|
|
31
|
+
reconnectDelayMs: 1500,
|
|
32
|
+
debug: false
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Connect and listen for events
|
|
36
|
+
client.connect({
|
|
37
|
+
connected: () => {
|
|
38
|
+
console.log('Connected to notification server');
|
|
39
|
+
},
|
|
40
|
+
disconnected: () => {
|
|
41
|
+
console.log('Disconnected from server');
|
|
42
|
+
},
|
|
43
|
+
listNotifications: (notifications) => {
|
|
44
|
+
console.log('Received notifications:', notifications);
|
|
45
|
+
},
|
|
46
|
+
newNotification: (notification) => {
|
|
47
|
+
console.log('New notification:', notification);
|
|
48
|
+
},
|
|
49
|
+
error: (error) => {
|
|
50
|
+
console.error('Connection error:', error);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Configuration Options
|
|
56
|
+
|
|
57
|
+
| Option | Type | Default | Description |
|
|
58
|
+
|--------|------|---------|-------------|
|
|
59
|
+
| `url` | `string` | *required* | WebSocket server URL |
|
|
60
|
+
| `clientId` | `string` | *required* | Unique client identifier |
|
|
61
|
+
| `token` | `string` | `undefined` | Optional authentication token |
|
|
62
|
+
| `reconnect` | `boolean` | `true` | Enable automatic reconnection |
|
|
63
|
+
| `reconnectDelayMs` | `number` | `1500` | Delay before reconnection attempt (ms) |
|
|
64
|
+
| `debug` | `boolean` | `false` | Enable debug logging |
|
|
65
|
+
|
|
66
|
+
## API Reference
|
|
67
|
+
|
|
68
|
+
### Events
|
|
69
|
+
|
|
70
|
+
Listen to events using the `connect()` method or by using `.on()`:
|
|
71
|
+
|
|
72
|
+
#### Lifecycle Events
|
|
73
|
+
- `connected` - Fired when WebSocket connection is established
|
|
74
|
+
- `disconnected` - Fired when WebSocket connection is closed
|
|
75
|
+
- `error` - Fired when an error occurs
|
|
76
|
+
|
|
77
|
+
#### Notification Events
|
|
78
|
+
- `listNotifications` - Receives list of notifications
|
|
79
|
+
- `newNotification` - Receives a new notification
|
|
80
|
+
- `listConfigurations` - Receives notification configurations
|
|
81
|
+
|
|
82
|
+
### Actions
|
|
83
|
+
|
|
84
|
+
Send actions to the server to manage notifications:
|
|
85
|
+
|
|
86
|
+
#### Mark as Read
|
|
87
|
+
```typescript
|
|
88
|
+
// Mark all notifications as read
|
|
89
|
+
client.markAsRead();
|
|
90
|
+
|
|
91
|
+
// Mark all notifications from a specific app as read
|
|
92
|
+
client.markAppAsRead('app-id');
|
|
93
|
+
|
|
94
|
+
// Mark all notifications in a group as read
|
|
95
|
+
client.markGroupAsRead('app-id', 'group-key');
|
|
96
|
+
|
|
97
|
+
// Mark a specific notification as read
|
|
98
|
+
client.markNotificationAsRead('notification-id');
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Delete Notifications
|
|
102
|
+
```typescript
|
|
103
|
+
// Delete all notifications
|
|
104
|
+
client.deleteNotifications();
|
|
105
|
+
|
|
106
|
+
// Delete all notifications from a specific app
|
|
107
|
+
client.deleteAppNotifications('app-id');
|
|
108
|
+
|
|
109
|
+
// Delete all notifications in a group
|
|
110
|
+
client.deleteGroupNotifications('app-id', 'group-key');
|
|
111
|
+
|
|
112
|
+
// Delete a specific notification
|
|
113
|
+
client.deleteNotification('notification-id');
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### Other Actions
|
|
117
|
+
```typescript
|
|
118
|
+
// Reload all notifications from server
|
|
119
|
+
client.reloadNotifications();
|
|
120
|
+
|
|
121
|
+
// Enable or disable notifications
|
|
122
|
+
client.setNotificationStatus(true); // enable
|
|
123
|
+
client.setNotificationStatus(false); // disable
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Custom Actions
|
|
127
|
+
|
|
128
|
+
For custom actions not covered by convenience methods:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
client.emitAction('customAction', { customData: 'value' });
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Advanced Usage
|
|
135
|
+
|
|
136
|
+
### Event Listeners
|
|
137
|
+
|
|
138
|
+
You can add event listeners dynamically:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
client.on('newNotification', (notification) => {
|
|
142
|
+
console.log('New notification received:', notification);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
client.on('error', (error) => {
|
|
146
|
+
console.error('Error occurred:', error);
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Manual Connection Management
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
const client = new R2NotifyClient({
|
|
154
|
+
url: 'wss://your-server.com',
|
|
155
|
+
clientId: 'user-123',
|
|
156
|
+
reconnect: false // Disable auto-reconnect
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Connect manually
|
|
160
|
+
client.connect();
|
|
161
|
+
|
|
162
|
+
// Close connection
|
|
163
|
+
client.close();
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### TypeScript Support
|
|
167
|
+
|
|
168
|
+
The package is fully typed. Import types as needed:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import type {
|
|
172
|
+
NotificationMessage,
|
|
173
|
+
NotificationApp,
|
|
174
|
+
NotificationGroup,
|
|
175
|
+
NotificationConfig,
|
|
176
|
+
R2NotifyClientOptions
|
|
177
|
+
} from 'r2-notify-client';
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Data Types
|
|
181
|
+
|
|
182
|
+
### NotificationMessage
|
|
183
|
+
```typescript
|
|
184
|
+
interface NotificationMessage {
|
|
185
|
+
id: string;
|
|
186
|
+
appId: string;
|
|
187
|
+
userId: string;
|
|
188
|
+
groupKey: string;
|
|
189
|
+
message: string;
|
|
190
|
+
status: "success" | "error" | "warning" | "info";
|
|
191
|
+
readStatus: boolean;
|
|
192
|
+
createdAt: string;
|
|
193
|
+
updatedAt: string;
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### NotificationGroup
|
|
198
|
+
```typescript
|
|
199
|
+
interface NotificationGroup {
|
|
200
|
+
groupKey: string;
|
|
201
|
+
latest: number;
|
|
202
|
+
unread: number;
|
|
203
|
+
items: NotificationMessage[];
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### NotificationApp
|
|
208
|
+
```typescript
|
|
209
|
+
interface NotificationApp {
|
|
210
|
+
appId: string;
|
|
211
|
+
latest: number;
|
|
212
|
+
unread: number;
|
|
213
|
+
groups: NotificationGroup[];
|
|
214
|
+
total: number;
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Examples
|
|
219
|
+
|
|
220
|
+
### React Integration
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { useEffect, useState } from 'react';
|
|
224
|
+
import { R2NotifyClient } from 'r2-notify-client';
|
|
225
|
+
|
|
226
|
+
function NotificationComponent() {
|
|
227
|
+
const [notifications, setNotifications] = useState([]);
|
|
228
|
+
const [client] = useState(() => new R2NotifyClient({
|
|
229
|
+
url: 'wss://your-server.com',
|
|
230
|
+
clientId: 'user-123'
|
|
231
|
+
}));
|
|
232
|
+
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
client.connect({
|
|
235
|
+
listNotifications: (data) => setNotifications(data),
|
|
236
|
+
newNotification: (notification) => {
|
|
237
|
+
setNotifications(prev => [...prev, notification]);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
return () => client.close();
|
|
242
|
+
}, [client]);
|
|
243
|
+
|
|
244
|
+
return (
|
|
245
|
+
<div>
|
|
246
|
+
{notifications.map(notif => (
|
|
247
|
+
<div key={notif.id}>
|
|
248
|
+
{notif.message}
|
|
249
|
+
<button onClick={() => client.markNotificationAsRead(notif.id)}>
|
|
250
|
+
Mark as Read
|
|
251
|
+
</button>
|
|
252
|
+
</div>
|
|
253
|
+
))}
|
|
254
|
+
</div>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Vue Integration
|
|
260
|
+
|
|
261
|
+
```vue
|
|
262
|
+
<script setup>
|
|
263
|
+
import { ref, onMounted, onUnmounted } from 'vue';
|
|
264
|
+
import { R2NotifyClient } from 'r2-notify-client';
|
|
265
|
+
|
|
266
|
+
const notifications = ref([]);
|
|
267
|
+
const client = new R2NotifyClient({
|
|
268
|
+
url: 'wss://your-server.com',
|
|
269
|
+
clientId: 'user-123'
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
onMounted(() => {
|
|
273
|
+
client.connect({
|
|
274
|
+
listNotifications: (data) => {
|
|
275
|
+
notifications.value = data;
|
|
276
|
+
},
|
|
277
|
+
newNotification: (notification) => {
|
|
278
|
+
notifications.value.push(notification);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
onUnmounted(() => {
|
|
284
|
+
client.close();
|
|
285
|
+
});
|
|
286
|
+
</script>
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## License
|
|
290
|
+
|
|
291
|
+
MIT ÂĐ Sherantha Perera
|
|
292
|
+
|
|
293
|
+
## Keywords
|
|
294
|
+
|
|
295
|
+
notifications, websocket, client, real-time, r2-notify
|
|
296
|
+
|
|
297
|
+
## Contributing
|
|
298
|
+
|
|
299
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
300
|
+
|
|
301
|
+
## Support
|
|
302
|
+
|
|
303
|
+
For issues and questions, please open an issue on the GitHub repository.
|