@pocketping/sdk-node 0.1.0 → 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/README.md ADDED
@@ -0,0 +1,305 @@
1
+ # PocketPing Node.js SDK
2
+
3
+ Node.js SDK for PocketPing - real-time customer chat with mobile notifications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @pocketping/sdk-node
9
+
10
+ # Or with pnpm
11
+ pnpm add @pocketping/sdk-node
12
+
13
+ # Or with yarn
14
+ yarn add @pocketping/sdk-node
15
+ ```
16
+
17
+ ## Quick Start with Express
18
+
19
+ ```typescript
20
+ import express from 'express';
21
+ import { createServer } from 'http';
22
+ import { PocketPing } from '@pocketping/sdk-node';
23
+
24
+ const app = express();
25
+ const server = createServer(app);
26
+
27
+ app.use(express.json());
28
+
29
+ // Initialize PocketPing
30
+ const pp = new PocketPing({
31
+ welcomeMessage: 'Hi! How can we help you today?',
32
+ onNewSession: (session) => {
33
+ console.log(`New session: ${session.id}`);
34
+ },
35
+ onMessage: (message, session) => {
36
+ console.log(`Message from ${message.sender}: ${message.content}`);
37
+ },
38
+ });
39
+
40
+ // Mount PocketPing routes
41
+ app.use('/pocketping', pp.middleware());
42
+
43
+ // Attach WebSocket for real-time communication
44
+ pp.attachWebSocket(server);
45
+
46
+ server.listen(3000, () => {
47
+ console.log('Server running on http://localhost:3000');
48
+ });
49
+ ```
50
+
51
+ ## Configuration Options
52
+
53
+ ```typescript
54
+ const pp = new PocketPing({
55
+ // Welcome message shown to new visitors
56
+ welcomeMessage: 'Hi! How can we help you?',
57
+
58
+ // Callbacks
59
+ onNewSession: (session) => { /* ... */ },
60
+ onMessage: (message, session) => { /* ... */ },
61
+ onEvent: (event, session) => { /* ... */ },
62
+
63
+ // Custom storage (default: in-memory)
64
+ storage: new MemoryStorage(),
65
+
66
+ // Bridge server for notifications (Telegram, Discord, Slack)
67
+ bridgeServerUrl: 'http://localhost:3001',
68
+
69
+ // Protocol version settings
70
+ protocolVersion: '1.0',
71
+ minSupportedVersion: '0.1',
72
+ });
73
+ ```
74
+
75
+ ## Architecture Options
76
+
77
+ ### 1. Embedded Mode (Simple)
78
+
79
+ SDK handles everything directly - best for single server deployments:
80
+
81
+ ```typescript
82
+ import { PocketPing } from '@pocketping/sdk-node';
83
+
84
+ const pp = new PocketPing({
85
+ welcomeMessage: 'Hello!',
86
+ });
87
+
88
+ app.use('/pocketping', pp.middleware());
89
+ pp.attachWebSocket(server);
90
+ ```
91
+
92
+ ### 2. Bridge Server Mode (Recommended)
93
+
94
+ SDK connects to a dedicated bridge server for notifications:
95
+
96
+ ```typescript
97
+ const pp = new PocketPing({
98
+ welcomeMessage: 'Hello!',
99
+ bridgeServerUrl: process.env.BRIDGE_SERVER_URL,
100
+ });
101
+ ```
102
+
103
+ The bridge server handles Telegram, Discord, and Slack integrations, keeping your main server lightweight.
104
+
105
+ ## Custom Storage
106
+
107
+ Implement the `Storage` interface for persistence:
108
+
109
+ ```typescript
110
+ import { Storage, Session, Message } from '@pocketping/sdk-node';
111
+
112
+ class PostgresStorage implements Storage {
113
+ async createSession(session: Session): Promise<void> {
114
+ // Your implementation
115
+ }
116
+
117
+ async getSession(sessionId: string): Promise<Session | null> {
118
+ // Your implementation
119
+ }
120
+
121
+ async saveMessage(message: Message): Promise<void> {
122
+ // Your implementation
123
+ }
124
+
125
+ async getMessages(sessionId: string, options?: { after?: string; limit?: number }): Promise<Message[]> {
126
+ // Your implementation
127
+ }
128
+
129
+ // ... implement other methods
130
+ }
131
+
132
+ const pp = new PocketPing({
133
+ storage: new PostgresStorage(),
134
+ });
135
+ ```
136
+
137
+ ## Events / Callbacks
138
+
139
+ ```typescript
140
+ const pp = new PocketPing({
141
+ onNewSession: (session) => {
142
+ console.log(`New session: ${session.id}`);
143
+ // Notify your team, log to analytics, etc.
144
+ },
145
+ onMessage: (message, session) => {
146
+ console.log(`Message from ${message.sender}: ${message.content}`);
147
+ },
148
+ onEvent: (event, session) => {
149
+ console.log(`Custom event: ${event.name}`, event.data);
150
+ },
151
+ });
152
+ ```
153
+
154
+ ## Custom Events
155
+
156
+ PocketPing supports bidirectional custom events between your website and backend.
157
+
158
+ ### Listening for Events (Widget -> Backend)
159
+
160
+ ```typescript
161
+ // Using callback in config
162
+ const pp = new PocketPing({
163
+ onEvent: (event, session) => {
164
+ console.log(`Event ${event.name} from session ${session.id}`);
165
+ console.log(`Data:`, event.data);
166
+ },
167
+ });
168
+
169
+ // Or using subscription
170
+ pp.onEvent('clicked_pricing', (event, session) => {
171
+ console.log(`User interested in: ${event.data?.plan}`);
172
+ });
173
+
174
+ // Subscribe to all events
175
+ pp.onEvent('*', (event, session) => {
176
+ console.log(`Event: ${event.name}`, event.data);
177
+ });
178
+ ```
179
+
180
+ ### Sending Events (Backend -> Widget)
181
+
182
+ ```typescript
183
+ // Send to a specific session
184
+ await pp.emitEvent('session-123', 'show_offer', {
185
+ discount: 20,
186
+ code: 'SAVE20',
187
+ });
188
+
189
+ // Broadcast to all connected sessions
190
+ await pp.broadcastEvent('announcement', {
191
+ message: 'New feature launched!',
192
+ });
193
+ ```
194
+
195
+ ## User Identification
196
+
197
+ Track and identify users across sessions:
198
+
199
+ ```typescript
200
+ // On the frontend (widget)
201
+ PocketPing.identify({
202
+ userId: 'user_123',
203
+ email: 'john@example.com',
204
+ name: 'John Doe',
205
+ plan: 'pro',
206
+ });
207
+
208
+ // Get current identity
209
+ const identity = PocketPing.getIdentity();
210
+
211
+ // Reset identity (e.g., on logout)
212
+ PocketPing.reset();
213
+ ```
214
+
215
+ User identity is automatically included in session metadata and forwarded to bridges.
216
+
217
+ ## Operator Presence
218
+
219
+ Control operator online status:
220
+
221
+ ```typescript
222
+ // Set operator as online
223
+ pp.setOperatorOnline(true);
224
+
225
+ // Set operator as offline
226
+ pp.setOperatorOnline(false);
227
+ ```
228
+
229
+ When using the bridge server, presence is managed automatically via Telegram/Discord/Slack commands.
230
+
231
+ ## API Reference
232
+
233
+ ### PocketPing Class
234
+
235
+ | Method | Description |
236
+ |--------|-------------|
237
+ | `middleware()` | Returns Express middleware for HTTP routes |
238
+ | `attachWebSocket(server)` | Attaches WebSocket handler for real-time communication |
239
+ | `setOperatorOnline(online)` | Sets operator online/offline status |
240
+ | `onEvent(name, callback)` | Subscribe to custom events |
241
+ | `offEvent(name, callback)` | Unsubscribe from custom events |
242
+ | `emitEvent(sessionId, name, data)` | Send event to specific session |
243
+ | `broadcastEvent(name, data)` | Broadcast event to all sessions |
244
+ | `getSession(sessionId)` | Get session by ID |
245
+ | `getMessages(sessionId, options)` | Get messages for a session |
246
+
247
+ ### Types
248
+
249
+ ```typescript
250
+ interface Session {
251
+ id: string;
252
+ visitorId: string;
253
+ metadata: SessionMetadata;
254
+ createdAt: Date;
255
+ lastActivity: Date;
256
+ status: 'active' | 'closed';
257
+ }
258
+
259
+ interface Message {
260
+ id: string;
261
+ sessionId: string;
262
+ sender: 'visitor' | 'operator' | 'system' | 'ai';
263
+ content: string;
264
+ timestamp: Date;
265
+ metadata?: Record<string, unknown>;
266
+ }
267
+
268
+ interface CustomEvent {
269
+ name: string;
270
+ data?: Record<string, unknown>;
271
+ timestamp: Date;
272
+ sessionId?: string;
273
+ }
274
+ ```
275
+
276
+ ## Widget Integration
277
+
278
+ Add the widget to your website:
279
+
280
+ ```html
281
+ <script src="https://unpkg.com/@pocketping/widget"></script>
282
+ <script>
283
+ PocketPing.init({
284
+ endpoint: '/pocketping',
285
+ theme: 'light', // or 'dark'
286
+ primaryColor: '#667eea',
287
+ });
288
+ </script>
289
+ ```
290
+
291
+ Or via npm:
292
+
293
+ ```typescript
294
+ import { init } from '@pocketping/widget';
295
+
296
+ init({
297
+ endpoint: '/pocketping',
298
+ theme: 'dark',
299
+ primaryColor: '#667eea',
300
+ });
301
+ ```
302
+
303
+ ## License
304
+
305
+ MIT