@datonow/sdk
v0.1.8
Published
JavaScript/TypeScript client SDK for Datonow — real-time notification delivery via WebSocket.
Maintainers
Readme
@datonow/sdk
JavaScript/TypeScript client SDK for the Datonow notification platform. Real-time delivery via WebSocket with a REST fallback — zero runtime dependencies.
Installation
npm install @datonow/sdk
# or
pnpm add @datonow/sdkQuick start: Frontend (Consumer)
import { Datonow } from '@datonow/sdk';
const client = new Datonow({
appId: 'YOUR_APP_ID',
publicToken: 'YOUR_PUBLIC_TOKEN',
recipientId: 'user-123', // the logged-in user
endpoint: 'wss://yourdomain.com', // or ws://localhost:8080 locally
});
// Real-time
client.on('notification', (notif) => {
console.log('New notification:', notif.title);
});
client.on('unread_count', (count) => {
updateBadge(count);
});
// REST
const { notifications, unread_count } = await client.list({ limit: 20 });
await client.markRead(['uuid-1', 'uuid-2']);
const count = await client.unreadCount();
// Cleanup
client.disconnect();Quick start: Backend (Producer)
import { DatonowProducer } from '@datonow/sdk';
const producer = new DatonowProducer({
appId: 'YOUR_APP_ID',
secretToken: 'YOUR_SECRET_TOKEN',
endpoint: 'https://yourdomain.com', // or http://localhost:8080 locally
});
await producer.publish({
eventType: 'order.completed',
recipientId: 'user-123',
title: 'Order Shipped',
body: 'Your order #456 is on its way.',
});Configuration
Consumer (Datonow)
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| appId | string | required | Your Datonow application ID |
| publicToken | string | required | Your application's public token |
| recipientId | string | required | The user ID to subscribe for |
| endpoint | string | ws://localhost:8080 | WebSocket / API base URL |
| autoConnect | boolean | true | Auto-open WebSocket on construction |
| initialReconnectDelay | number | 1000 | First reconnect delay (ms) |
| maxReconnectDelay | number | 30000 | Maximum reconnect delay (ms) |
Producer (DatonowProducer)
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| appId | string | required | Your Datonow application ID |
| secretToken | string | required | Your application's secret token |
| endpoint | string | http://localhost:8080 | API base URL |
Events
| Event | Payload | Description |
|-------|---------|-------------|
| notification | Notification | New notification pushed via WebSocket |
| unread_count | number | Updated unread count from server |
| connected | — | WebSocket connected and authenticated (fired after auth_ok) |
| disconnected | CloseEvent | WebSocket closed (auto-reconnect scheduled) |
| error | Error | Unrecoverable error |
Authentication
The publicToken (Consumer) is sent securely as the Sec-WebSocket-Protocol header during the connection upgrade. It never appears in the URL query string, protecting it from proxy access logs.
The secretToken (Producer) is sent as the X-Datonow-Secret-Token header for all backend REST calls.
REST API
// Paginated notification list
const result = await client.list({ limit: 20, cursor: 'last-uuid' });
// result.notifications: Notification[]
// result.next_cursor: string | null
// result.unread_count: number
// Mark as read
await client.markRead(['id-1', 'id-2']);
// Unread count only
const count = await client.unreadCount();Reconnect strategy
The SDK automatically reconnects with exponential backoff:
1s → 2s → 4s → 8s → 16s → 30s (max) → 30s → …Call client.disconnect() to stop reconnecting permanently.
Examples
| Directory | Description |
|-----------|-------------|
| examples/vanilla/ | Plain HTML + ES modules — no bundler needed |
| examples/nextjs/ | React hook + NotificationBell component for Next.js App Router |
License
MIT
