@chitchat/sdk-web 0.1.0-dev.1 → 0.1.0-dev.2

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/README.md +127 -2
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,5 +1,130 @@
1
1
  # ChitChat Web SDK
2
2
 
3
- `createChitChatWebClient` always uses WSS. It authenticates through the first binary Protobuf frame, never by placing a session token in a URL. Supply the generated Protobuf codec and a function that fetches a short-lived project-user session token. Plain `ws://` is rejected except for an explicit `allowInsecureWs: true` localhost-only development configuration.
3
+ `@chitchat/sdk-web` connects a browser to ChitChat realtime using secure WebSocket (**WSS**) and binary Protobuf frames. Browsers cannot use raw TCP, so this package never attempts it.
4
4
 
5
- Raw TCP is not available in browsers and is never attempted by this module.
5
+ This package is currently a **development prerelease**. Pin the version you have tested before shipping an application.
6
+
7
+ ## What you need first
8
+
9
+ 1. A backend session endpoint created with `@chitchat/sdk-server`. It authenticates the person in your product and returns a short-lived `{ token, expiresAt }` response.
10
+ 2. An HTTPS realtime endpoint, for example `https://realtime.example.com`. The SDK converts it to `wss://realtime.example.com`.
11
+ 3. A generated Protobuf codec adapter that is compatible with the ChitChat gateway’s current `chitchat.protobuf.v1` envelope and authentication frame.
12
+ 4. A durable browser outbox implementation (IndexedDB) if messages must survive page reloads.
13
+
14
+ ```bash
15
+ npm install @chitchat/sdk-core@development @chitchat/sdk-web@development
16
+ ```
17
+
18
+ Do not install `@chitchat/sdk-server` in a browser application and never expose a ChitChat server API key in JavaScript, public environment variables, source maps, or logs.
19
+
20
+ ## Minimal connection setup
21
+
22
+ ```js
23
+ import { createBackendTokenProvider } from '@chitchat/sdk-core';
24
+ import { createChitChatWebClient } from '@chitchat/sdk-web';
25
+ import { chitchatEnvelopeCodec } from './generated/chitchat-envelope-codec.js';
26
+
27
+ const tokenProvider = createBackendTokenProvider({
28
+ endpoint: 'https://api.example.com/api/chitchat/session',
29
+ credentials: 'include',
30
+ getBody: () => ({ deviceId: getStableInstallationId() })
31
+ });
32
+
33
+ const client = createChitChatWebClient({
34
+ endpoint: 'https://realtime.example.com',
35
+ tokenProvider,
36
+ // This must encode/decode the deployed Protobuf envelope—not JSON.
37
+ protobuf: chitchatEnvelopeCodec,
38
+ storage: indexedDbOutbox
39
+ });
40
+
41
+ client.on('connected', () => setRealtimeState('connected'));
42
+ client.on('disconnected', () => setRealtimeState('reconnecting'));
43
+ client.on('message', (frame) => handleChitChatFrame(frame));
44
+ client.on('error', (error) => reportNonSensitiveRealtimeError(error));
45
+
46
+ await client.connect();
47
+ ```
48
+
49
+ When the user signs out or switches accounts, close the old client before creating a new one:
50
+
51
+ ```js
52
+ await client.close();
53
+ tokenProvider.invalidate();
54
+ ```
55
+
56
+ ## Protobuf codec requirement
57
+
58
+ Pass an object with these two functions:
59
+
60
+ ```js
61
+ const chitchatEnvelopeCodec = {
62
+ encode(frame) {
63
+ // Convert the SDK frame to the exact generated Protobuf envelope bytes.
64
+ // The first auth frame must match the gateway handshake schema.
65
+ return generatedEnvelope.encode(frame).finish();
66
+ },
67
+ decode(binary) {
68
+ // Convert ArrayBuffer / binary data from the gateway into an SDK frame.
69
+ return generatedEnvelope.decode(new Uint8Array(binary));
70
+ }
71
+ };
72
+ ```
73
+
74
+ Do not use a JSON mock codec in production. This SDK release exposes the adapter boundary; the generated ChitChat envelope codec and the production handshake compatibility test must come from the deployed platform protocol. If that codec is unavailable, do not enable realtime for users yet.
75
+
76
+ ## Sending and receiving
77
+
78
+ ```js
79
+ const localMessageId = await client.send('chat.message', {
80
+ conversationId: 'conversation_123',
81
+ body: 'Hello'
82
+ });
83
+
84
+ client.on('ack', (messageId) => {
85
+ // The gateway acknowledged a queued outgoing record.
86
+ markMessageTransportAcknowledged(messageId);
87
+ });
88
+ ```
89
+
90
+ `send()` queues the outgoing record; it is not a peer-delivered or read receipt. Map the platform’s message/receipt frames to your UI separately.
91
+
92
+ ## WSS and local development
93
+
94
+ - `https://...` endpoints are converted to `wss://...`.
95
+ - `wss://...` endpoints are accepted directly.
96
+ - `http://...` and `ws://...` are rejected by default.
97
+ - For a loopback-only local test, opt in explicitly:
98
+
99
+ ```js
100
+ const client = createChitChatWebClient({
101
+ endpoint: 'http://127.0.0.1:5222',
102
+ tokenProvider,
103
+ protobuf: chitchatEnvelopeCodec,
104
+ allowInsecureWs: true
105
+ });
106
+ ```
107
+
108
+ This local exception works only for `localhost`, `127.0.0.1`, or `::1`; never enable it for LAN, staging, or production addresses.
109
+
110
+ ## Common errors
111
+
112
+ | Error | Fix |
113
+ | --- | --- |
114
+ | `Web realtime requires WSS` | Use an HTTPS/WSS realtime endpoint. Do not use TCP or non-local WS from a browser. |
115
+ | `WSS connection failed` | Check DNS, TLS certificate/hostname, gateway availability, session token, and gateway origin policy. |
116
+ | Connection opens then protocol errors | Your codec or first Protobuf auth frame does not match the deployed gateway schema. Regenerate/use the correct protocol adapter. |
117
+ | Messages disappear after reload | Supply an IndexedDB-backed `storage`; the default core store is memory-only. |
118
+ | Browser session endpoint is rejected | Verify CORS/cookie policy and authenticate the user in your own backend before issuing a ChitChat session. |
119
+
120
+ ## Security checklist
121
+
122
+ - Keep the server API key in your backend only.
123
+ - Use HTTPS for your token endpoint and WSS for realtime.
124
+ - Do not put session tokens in URLs, logs, analytics, or error reports.
125
+ - Restrict the backend CORS policy to your actual web origins and protect the session endpoint against CSRF when using cookies.
126
+ - Call `close()` and invalidate the token provider at logout.
127
+
128
+ ## Current scope
129
+
130
+ The Web SDK handles a WSS transport and the reliable client lifecycle. Chat UI, conversation APIs, attachments, read receipts, WebRTC media, meetings, streaming, notifications, and agents require their own deployed platform APIs and protocol handlers.
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@chitchat/sdk-web",
3
- "version": "0.1.0-dev.1",
3
+ "version": "0.1.0-dev.2",
4
4
  "main": "src/index.js",
5
5
  "exports": "./src/index.js",
6
6
  "files": ["src", "README.md", "LICENSE"],
7
7
  "engines": { "node": ">=18" },
8
- "dependencies": { "@chitchat/sdk-core": "0.1.0-dev.1" },
8
+ "dependencies": { "@chitchat/sdk-core": "0.1.0-dev.2" },
9
9
  "license": "UNLICENSED",
10
10
  "publishConfig": { "access": "public", "tag": "development" }
11
11
  }