openkbs-pulse 1.0.2 → 1.0.5

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 (2) hide show
  1. package/package.json +13 -3
  2. package/server.js +81 -0
package/package.json CHANGED
@@ -1,12 +1,17 @@
1
1
  {
2
2
  "name": "openkbs-pulse",
3
- "version": "1.0.2",
3
+ "version": "1.0.5",
4
4
  "description": "Real-time WebSocket SDK for OpenKBS",
5
5
  "main": "pulse.js",
6
6
  "types": "pulse.d.ts",
7
+ "exports": {
8
+ ".": "./pulse.js",
9
+ "./server": "./server.js"
10
+ },
7
11
  "files": [
8
12
  "pulse.js",
9
- "pulse.d.ts"
13
+ "pulse.d.ts",
14
+ "server.js"
10
15
  ],
11
16
  "scripts": {
12
17
  "deploy:cdn": "./deploy.sh",
@@ -14,7 +19,12 @@
14
19
  "publish:minor": "npm version minor && npm publish --access public",
15
20
  "publish:major": "npm version major && npm publish --access public"
16
21
  },
17
- "keywords": ["websocket", "realtime", "pubsub", "openkbs"],
22
+ "keywords": [
23
+ "websocket",
24
+ "realtime",
25
+ "pubsub",
26
+ "openkbs"
27
+ ],
18
28
  "author": "OpenKBS",
19
29
  "license": "MIT"
20
30
  }
package/server.js ADDED
@@ -0,0 +1,81 @@
1
+ /**
2
+ * OpenKBS Pulse - Server SDK
3
+ *
4
+ * Usage:
5
+ * import pulse from 'openkbs-pulse/server';
6
+ *
7
+ * await pulse.publish('posts', 'new_post', { post });
8
+ * const { count, members } = await pulse.presence('posts');
9
+ */
10
+
11
+ const PULSE_API = process.env.PULSE_API_URL || 'https://kb.openkbs.com';
12
+
13
+ const pulse = {
14
+ /**
15
+ * Publish a message to a channel
16
+ * @param {string} channel - Channel name
17
+ * @param {string} event - Event type
18
+ * @param {object} data - Event data
19
+ */
20
+ async publish(channel, event, data) {
21
+ const kbId = process.env.OPENKBS_KB_ID;
22
+ const apiKey = process.env.OPENKBS_API_KEY;
23
+
24
+ if (!kbId || !apiKey) {
25
+ console.log('[Pulse] Not configured, skipping publish');
26
+ return { success: false, error: 'not_configured' };
27
+ }
28
+
29
+ try {
30
+ const res = await fetch(PULSE_API, {
31
+ method: 'POST',
32
+ headers: { 'Content-Type': 'application/json' },
33
+ body: JSON.stringify({
34
+ action: 'pulsePublish',
35
+ kbId,
36
+ apiKey,
37
+ channel,
38
+ message: { type: event, ...data }
39
+ })
40
+ });
41
+ const result = await res.json();
42
+ return { success: true, ...result };
43
+ } catch (e) {
44
+ console.error('[Pulse] Publish error:', e.message);
45
+ return { success: false, error: e.message };
46
+ }
47
+ },
48
+
49
+ /**
50
+ * Get presence info for a channel
51
+ * @param {string} channel - Channel name
52
+ */
53
+ async presence(channel) {
54
+ const kbId = process.env.OPENKBS_KB_ID;
55
+ const apiKey = process.env.OPENKBS_API_KEY;
56
+
57
+ if (!kbId || !apiKey) {
58
+ return { count: 0, members: [] };
59
+ }
60
+
61
+ try {
62
+ const res = await fetch(PULSE_API, {
63
+ method: 'POST',
64
+ headers: { 'Content-Type': 'application/json' },
65
+ body: JSON.stringify({
66
+ action: 'pulsePresence',
67
+ kbId,
68
+ apiKey,
69
+ channel
70
+ })
71
+ });
72
+ return await res.json();
73
+ } catch (e) {
74
+ console.error('[Pulse] Presence error:', e.message);
75
+ return { count: 0, members: [] };
76
+ }
77
+ }
78
+ };
79
+
80
+ module.exports = pulse;
81
+ module.exports.default = pulse;