@sockethub/client 5.0.0-alpha.10 → 5.0.0-alpha.12

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 CHANGED
@@ -1,55 +1,65 @@
1
1
  # @sockethub/client
2
2
 
3
- JavaScript client for [Sockethub](https://github.com/sockethub/sockethub) - a protocol
4
- gateway that translates ActivityStreams messages into various protocols (XMPP, IRC, RSS, etc.).
3
+ The Sockethub client is a small JavaScript SDK for app developers to connect to a
4
+ Sockethub server via Socket.IO and send/receive ActivityStreams messages. It works
5
+ in both Node.js and browsers, and provides helpers for ActivityStreams along with
6
+ automatic reconnection and credential replay.
5
7
 
6
- Works in Node.js and browsers.
8
+ ## What's Included
9
+
10
+ - `SockethubClient` for connection and message handling
11
+ - `ActivityStreams` helpers and validation utilities
12
+ - `contextFor(platform)` builds canonical `@context` arrays from server metadata
13
+ - `ready()` promise and `ready`/`init_error` observability events
14
+ - Automatic outbound queueing until initialization is complete
15
+ - Auto-replay of credentials and connections on reconnect
16
+ - A browser-ready bundle in `dist/`
7
17
 
8
18
  ## Install
9
19
 
10
20
  ### Node.js
11
21
 
12
- `$ npm install @sockethub/client`
22
+ `$ npm install @sockethub/client socket.io-client`
13
23
 
14
24
  ### Bun
15
25
 
16
- `$ bun install @sockethub/client`
17
-
18
- #### CommonJS
19
-
20
- ```javascript
21
- const SockethubClient = require('@sockethub/client');
22
- const io = require('@socket.io-client');
23
- const SOCKETHUB_SERVER = 'http://localhost:10550';
24
- const sc = SockethubClient(io(SOCKETHUB_SERVER));
25
- ```
26
-
27
- #### ESM
26
+ `$ bun install @sockethub/client socket.io-client`
28
27
 
29
28
  ```javascript
30
29
  import SockethubClient from '@sockethub/client';
31
- import { io } from '@socket.io-client';
30
+ import { io } from 'socket.io-client';
32
31
  const SOCKETHUB_SERVER = 'http://localhost:10550';
33
- const sc = SockethubClient(io(SOCKETHUB_SERVER));
32
+ const sc = new SockethubClient(io(SOCKETHUB_SERVER));
34
33
  ```
35
34
 
36
35
  ### Browser
37
36
 
38
- The browser bundle is available in the dist folder:
37
+ Two browser builds are published in `dist/`:
39
38
 
40
- ```
41
- import '@sockethub/client/dist/sockethub-client.js';
42
- ```
39
+ - `dist/sockethub-client.browser.js` is the IIFE/global build for plain `<script>` tags
40
+ - `dist/sockethub-client.js` is the ESM build for `<script type="module">`
43
41
 
44
- You can place it somewhere accessible from the web and include
45
- it via a `script` tag.
42
+ If you are serving assets from a Sockethub server, use the pre-copied global build
43
+ at `/sockethub-client.js` along with `/socket.io.js`:
46
44
 
45
+ ```html
46
+ <script src="/socket.io.js"></script>
47
+ <script src="/sockethub-client.js"></script>
47
48
  ```
48
- <script src="http://example.com/sockethub-client.js" type="module"></script>
49
- ```
50
49
 
51
- Once included in a web-page, the `SockethubClient` base object
52
- should be on the global scope.
50
+ These scripts set `io` and `SockethubClient` as globals.
51
+
52
+ If you are hosting the package files yourself and want ESM instead, serve
53
+ `dist/sockethub-client.js` and import it from a module script:
54
+
55
+ ```html
56
+ <script type="module">
57
+ import SockethubClient from '/dist/sockethub-client.js';
58
+ import { io } from '/socket.io.esm.min.js';
59
+
60
+ const sc = new SockethubClient(io('http://localhost:10550', { path: '/sockethub' }));
61
+ </script>
62
+ ```
53
63
 
54
64
  ## Quick Start
55
65
 
@@ -58,21 +68,108 @@ import SockethubClient from '@sockethub/client';
58
68
  import { io } from 'socket.io-client';
59
69
 
60
70
  const socket = io('http://localhost:10550', { path: '/sockethub' });
61
- const sc = new SockethubClient(socket);
71
+ const sc = new SockethubClient(socket, { initTimeoutMs: 5000 });
62
72
 
63
73
  sc.socket.on('message', (msg) => console.log(msg));
74
+ sc.socket.on('ready', (info) => {
75
+ console.log('Sockethub ready:', info.reason, info.sockethubVersion,
76
+ info.platforms.map((p) => ({ id: p.id, version: p.version })));
77
+ });
78
+ sc.socket.on('init_error', (e) => {
79
+ console.warn('Sockethub init issue:', e.error);
80
+ });
81
+
82
+ // Wait for schema registry, then send a message
83
+ try {
84
+ await sc.ready();
85
+ sc.socket.emit('message', {
86
+ '@context': sc.contextFor('dummy'),
87
+ type: 'echo',
88
+ actor: { id: 'test@dummy', type: 'person' },
89
+ object: { type: 'message', content: 'hello world' }
90
+ }, (ack) => {
91
+ if (ack?.error) {
92
+ console.error('Send failed:', ack.error);
93
+ return;
94
+ }
95
+ console.log('Ack:', ack);
96
+ });
97
+ } catch (err) {
98
+ console.error('Sockethub failed to initialize:', err);
99
+ }
64
100
  ```
65
101
 
66
102
  See the [Client Guide](../../docs/client-guide.md) for detailed usage and examples.
67
103
 
68
104
  ## API
69
105
 
70
- - **`new SockethubClient(socket)`** - Create client instance
71
- - **`sc.socket.emit(event, data)`** - Send messages
106
+ - **`new SockethubClient(socket, options?)`** - Create client instance with optional init/queue settings
107
+ - **`sc.ready(timeoutMs?)`** - Wait for initialization to complete
108
+ - **`sc.isReady()`** - Check whether client is initialized
109
+ - **`sc.getInitState()`** - Return `"idle" | "initializing" | "ready" | "init_error" | "closed"`
110
+ - **`sc.contextFor(platform)`** - Build canonical `@context` for a platform from server registry
111
+ - **`sc.getRegisteredPlatforms()`** - Get server-registered platforms/contexts
112
+ - **`sc.getRegisteredBaseContexts()`** - Get AS2 and Sockethub base context URLs
113
+ - **`sc.getPlatformSchema(platform, schemaType?)`** - Get platform JSON schema
114
+ - **`sc.validateActivity(activity)`** - Validate against registered schemas
115
+ - **`sc.socket.emit(event, data, callback)`** - Send messages (queued until ready)
72
116
  - **`sc.socket.on(event, handler)`** - Listen for messages
73
117
  - **`sc.clearCredentials()`** - Clear stored credentials
74
118
  - **`sc.ActivityStreams`** - ActivityStreams library
75
119
 
120
+ ## Result Handling
121
+
122
+ For client-initiated requests, pass a callback to `emit` and treat an `error`
123
+ field as failure:
124
+
125
+ ```javascript
126
+ sc.socket.emit("message", {
127
+ type: "echo",
128
+ "@context": sc.contextFor("dummy"),
129
+ actor: { id: "test", type: "person" },
130
+ object: { type: "message", content: "Hello!" },
131
+ }, (result) => {
132
+ if (result?.error) {
133
+ console.error("Sockethub request failed:", result.error);
134
+ return;
135
+ }
136
+
137
+ console.log("Sockethub request succeeded:", result);
138
+ });
139
+ ```
140
+
141
+ Also listen for ongoing platform events:
142
+
143
+ ```javascript
144
+ sc.socket.on("message", (msg) => {
145
+ console.log("incoming platform event:", msg);
146
+ });
147
+ ```
148
+
149
+ ## ActivityStreams Helpers
150
+
151
+ Define reusable objects via `ActivityStreams.Object.create(...)`, then build
152
+ streams with `ActivityStreams.Stream(...)`:
153
+
154
+ ```javascript
155
+ sc.ActivityStreams.Object.create({
156
+ id: "mynick",
157
+ type: "person",
158
+ name: "My IRC Nick",
159
+ });
160
+
161
+ const stream = sc.ActivityStreams.Stream({
162
+ type: "join",
163
+ "@context": sc.contextFor("irc"),
164
+ actor: "mynick",
165
+ target: { id: "#sockethub", type: "room" },
166
+ });
167
+
168
+ sc.socket.emit("message", stream, (result) => {
169
+ if (result?.error) console.error(result.error);
170
+ });
171
+ ```
172
+
76
173
  ## Security & State Management
77
174
 
78
175
  ### Automatic Reconnection