openkbs-pulse 1.0.5 → 1.0.7

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.
Files changed (3) hide show
  1. package/README.md +25 -0
  2. package/package.json +1 -1
  3. package/server.js +28 -0
package/README.md CHANGED
@@ -177,6 +177,31 @@ pulse.connect();
177
177
  </html>
178
178
  ```
179
179
 
180
+ ## Server SDK
181
+
182
+ For server-side usage (Elastic Functions, Node.js):
183
+
184
+ ```javascript
185
+ import pulse from 'openkbs-pulse/server';
186
+
187
+ // Get a token for client WebSocket auth
188
+ const { token, endpoint, region } = await pulse.getToken(kbId, apiKey, userId);
189
+
190
+ // Publish to channel
191
+ await pulse.publish('channel', 'event', { data });
192
+
193
+ // Get presence info
194
+ const { count, members } = await pulse.presence('channel');
195
+ ```
196
+
197
+ ### Server SDK Functions
198
+
199
+ | Function | Parameters | Description |
200
+ |----------|------------|-------------|
201
+ | `getToken(kbId, apiKey, userId)` | kbId, apiKey (required), userId (default: 'anonymous') | Get WebSocket auth token |
202
+ | `publish(channel, event, data)` | All required | Publish message to channel |
203
+ | `presence(channel)` | channel name | Get online count and members |
204
+
180
205
  ## License
181
206
 
182
207
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openkbs-pulse",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Real-time WebSocket SDK for OpenKBS",
5
5
  "main": "pulse.js",
6
6
  "types": "pulse.d.ts",
package/server.js CHANGED
@@ -11,6 +11,34 @@
11
11
  const PULSE_API = process.env.PULSE_API_URL || 'https://kb.openkbs.com';
12
12
 
13
13
  const pulse = {
14
+ /**
15
+ * Get a Pulse token for WebSocket authentication
16
+ * @param {string} kbId - KB ID (optional, uses env var)
17
+ * @param {string} apiKey - API Key (optional, uses env var)
18
+ * @param {string} userId - User identifier (default: 'anonymous')
19
+ * @returns {Promise<{token, endpoint, region, expiresIn}>}
20
+ */
21
+ async getToken(kbId, apiKey, userId = 'anonymous') {
22
+ const kb = kbId || process.env.OPENKBS_KB_ID;
23
+ const key = apiKey || process.env.OPENKBS_API_KEY;
24
+
25
+ if (!kb || !key) {
26
+ throw new Error('[Pulse] kbId and apiKey required');
27
+ }
28
+
29
+ const res = await fetch(PULSE_API, {
30
+ method: 'POST',
31
+ headers: { 'Content-Type': 'application/json' },
32
+ body: JSON.stringify({
33
+ action: 'createPulseToken',
34
+ kbId: kb,
35
+ apiKey: key,
36
+ userId
37
+ })
38
+ });
39
+ return res.json();
40
+ },
41
+
14
42
  /**
15
43
  * Publish a message to a channel
16
44
  * @param {string} channel - Channel name