openkbs-pulse 1.0.2 → 1.0.3

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 +7 -2
  2. package/server.js +82 -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.3",
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",
package/server.js ADDED
@@ -0,0 +1,82 @@
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
+ event,
39
+ data
40
+ })
41
+ });
42
+ const result = await res.json();
43
+ return { success: true, ...result };
44
+ } catch (e) {
45
+ console.error('[Pulse] Publish error:', e.message);
46
+ return { success: false, error: e.message };
47
+ }
48
+ },
49
+
50
+ /**
51
+ * Get presence info for a channel
52
+ * @param {string} channel - Channel name
53
+ */
54
+ async presence(channel) {
55
+ const kbId = process.env.OPENKBS_KB_ID;
56
+ const apiKey = process.env.OPENKBS_API_KEY;
57
+
58
+ if (!kbId || !apiKey) {
59
+ return { count: 0, members: [] };
60
+ }
61
+
62
+ try {
63
+ const res = await fetch(PULSE_API, {
64
+ method: 'POST',
65
+ headers: { 'Content-Type': 'application/json' },
66
+ body: JSON.stringify({
67
+ action: 'pulsePresence',
68
+ kbId,
69
+ apiKey,
70
+ channel
71
+ })
72
+ });
73
+ return await res.json();
74
+ } catch (e) {
75
+ console.error('[Pulse] Presence error:', e.message);
76
+ return { count: 0, members: [] };
77
+ }
78
+ }
79
+ };
80
+
81
+ module.exports = pulse;
82
+ module.exports.default = pulse;