@massalabs/gossip-sdk 0.0.2-dev.20260302182124 → 0.0.2-dev.20260303161452
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 +58 -36
- package/dist/services/message.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,15 +20,24 @@ npm install @massalabs/gossip-sdk
|
|
|
20
20
|
|
|
21
21
|
## Quick Start
|
|
22
22
|
|
|
23
|
-
The SDK
|
|
23
|
+
The SDK exposes a convenient singleton `gossipSdk` for most apps, and you can also create additional `GossipSdk` instances when you need multiple independent sessions:
|
|
24
24
|
|
|
25
25
|
```typescript
|
|
26
|
-
import {
|
|
26
|
+
import { gossipSdk, GossipSdk, SdkEventType } from '@massalabs/gossip-sdk';
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
// Use the singleton for most cases
|
|
29
|
+
const sdk = gossipSdk;
|
|
30
|
+
|
|
31
|
+
// Or create your own instance if you need multiple SDKs in the same process
|
|
32
|
+
// const sdk = await new GossipSdk().init({
|
|
33
|
+
// storage: { type: 'idb', name: 'gossip-db' },
|
|
34
|
+
// });
|
|
29
35
|
|
|
30
36
|
// 1. Initialize (optional config)
|
|
31
|
-
await sdk.init(
|
|
37
|
+
await sdk.init({
|
|
38
|
+
// protocolBaseUrl: 'https://api.usegossip.com', // optional, otherwise env/default is used
|
|
39
|
+
// storage: { type: 'idb', name: 'gossip-db' }, // optional, defaults to in-memory
|
|
40
|
+
});
|
|
32
41
|
|
|
33
42
|
// 2. Open session (login)
|
|
34
43
|
await sdk.openSession({
|
|
@@ -41,9 +50,12 @@ await sdk.openSession({
|
|
|
41
50
|
});
|
|
42
51
|
|
|
43
52
|
// 3. Use the SDK
|
|
44
|
-
const contacts = await sdk.contacts.list(
|
|
45
|
-
await sdk.discussions.
|
|
46
|
-
|
|
53
|
+
const contacts = await sdk.contacts.list();
|
|
54
|
+
await sdk.discussions.startByUserId(contactUserId, 'Alice', {
|
|
55
|
+
username: 'Alice',
|
|
56
|
+
message: 'Hello!',
|
|
57
|
+
});
|
|
58
|
+
await sdk.messages.sendText(contactUserId, 'Hi Alice!');
|
|
47
59
|
|
|
48
60
|
// 4. Listen to events
|
|
49
61
|
sdk.on(SdkEventType.MESSAGE_RECEIVED, msg => {
|
|
@@ -58,15 +70,15 @@ await sdk.closeSession();
|
|
|
58
70
|
|
|
59
71
|
### 1. Initialize
|
|
60
72
|
|
|
61
|
-
Call `init()` once at app startup. All options are optional:
|
|
73
|
+
Call `init()` once at app startup on your SDK instance (either the `gossipSdk` singleton or an instance of `GossipSdk`). All options inside the `GossipSdkInitOptions` object are optional:
|
|
62
74
|
|
|
63
75
|
```typescript
|
|
64
|
-
// Uses
|
|
65
|
-
await sdk.init();
|
|
76
|
+
// Uses VITE_GOSSIP_API_URL / GOSSIP_API_URL env vars, or defaults to https://api.usegossip.com/api
|
|
77
|
+
await sdk.init({});
|
|
66
78
|
|
|
67
79
|
// Or with explicit config
|
|
68
80
|
await sdk.init({
|
|
69
|
-
protocolBaseUrl: 'https://api.usegossip.com',
|
|
81
|
+
protocolBaseUrl: 'https://api.usegossip.com/api',
|
|
70
82
|
config: {
|
|
71
83
|
polling: {
|
|
72
84
|
enabled: true,
|
|
@@ -118,22 +130,27 @@ All services are available after `openSession()` is called.
|
|
|
118
130
|
```typescript
|
|
119
131
|
// Send a message
|
|
120
132
|
const result = await sdk.messages.send({
|
|
121
|
-
ownerUserId: sdk.userId,
|
|
122
|
-
contactUserId: contactId,
|
|
133
|
+
ownerUserId: sdk.userId, // inferred session owner
|
|
134
|
+
contactUserId: contactId, // encoded userId of the contact
|
|
123
135
|
content: 'Hello!',
|
|
124
136
|
type: MessageType.TEXT,
|
|
125
137
|
direction: MessageDirection.OUTGOING,
|
|
126
|
-
status: MessageStatus.
|
|
138
|
+
status: MessageStatus.WAITING_SESSION,
|
|
127
139
|
timestamp: new Date(),
|
|
128
140
|
});
|
|
129
141
|
|
|
142
|
+
// Or use the simplified helper
|
|
143
|
+
await sdk.messages.sendText(contactId, 'Hello!', {
|
|
144
|
+
metadata: { source: 'bot' },
|
|
145
|
+
});
|
|
146
|
+
|
|
130
147
|
// Fetch new messages from server
|
|
131
148
|
const fetchResult = await sdk.messages.fetch();
|
|
132
149
|
|
|
133
|
-
//
|
|
134
|
-
const
|
|
150
|
+
// Get all messages for a given contact
|
|
151
|
+
const messages = await sdk.messages.getMessages(contactId);
|
|
135
152
|
|
|
136
|
-
// Mark as read
|
|
153
|
+
// Mark a specific message (by DB ID) as read
|
|
137
154
|
await sdk.messages.markAsRead(messageId);
|
|
138
155
|
```
|
|
139
156
|
|
|
@@ -141,7 +158,10 @@ await sdk.messages.markAsRead(messageId);
|
|
|
141
158
|
|
|
142
159
|
```typescript
|
|
143
160
|
// Start a new discussion
|
|
144
|
-
const result = await sdk.discussions.
|
|
161
|
+
const result = await sdk.discussions.startByUserId(contactUserId, 'Alice', {
|
|
162
|
+
username: 'Alice',
|
|
163
|
+
message: 'Hi!',
|
|
164
|
+
});
|
|
145
165
|
|
|
146
166
|
// Accept an incoming discussion request
|
|
147
167
|
await sdk.discussions.accept(discussion);
|
|
@@ -153,34 +173,29 @@ await sdk.discussions.renew(contactUserId);
|
|
|
153
173
|
const status = sdk.discussions.getStatus(contactUserId);
|
|
154
174
|
|
|
155
175
|
// List all discussions
|
|
156
|
-
const discussions = await sdk.discussions.list(
|
|
176
|
+
const discussions = await sdk.discussions.list();
|
|
157
177
|
|
|
158
178
|
// Get a specific discussion
|
|
159
|
-
const discussion = await sdk.discussions.get(
|
|
179
|
+
const discussion = await sdk.discussions.get(contactUserId);
|
|
160
180
|
```
|
|
161
181
|
|
|
162
182
|
### Contacts
|
|
163
183
|
|
|
164
184
|
```typescript
|
|
165
185
|
// List all contacts
|
|
166
|
-
const contacts = await sdk.contacts.list(
|
|
186
|
+
const contacts = await sdk.contacts.list();
|
|
167
187
|
|
|
168
188
|
// Get a specific contact
|
|
169
|
-
const contact = await sdk.contacts.get(
|
|
189
|
+
const contact = await sdk.contacts.get(contactUserId);
|
|
170
190
|
|
|
171
191
|
// Add a new contact
|
|
172
|
-
const result = await sdk.contacts.add(
|
|
173
|
-
ownerUserId,
|
|
174
|
-
contactUserId,
|
|
175
|
-
'Alice',
|
|
176
|
-
publicKeys
|
|
177
|
-
);
|
|
192
|
+
const result = await sdk.contacts.add(contactUserId, 'Alice', publicKeys);
|
|
178
193
|
|
|
179
194
|
// Update contact name
|
|
180
|
-
await sdk.contacts.updateName(
|
|
195
|
+
await sdk.contacts.updateName(contactUserId, 'Alice Smith');
|
|
181
196
|
|
|
182
197
|
// Delete contact and all associated data
|
|
183
|
-
await sdk.contacts.delete(
|
|
198
|
+
await sdk.contacts.delete(contactUserId);
|
|
184
199
|
```
|
|
185
200
|
|
|
186
201
|
### Announcements
|
|
@@ -288,7 +303,7 @@ Full configuration options with defaults:
|
|
|
288
303
|
await sdk.init({
|
|
289
304
|
config: {
|
|
290
305
|
protocol: {
|
|
291
|
-
baseUrl: 'https://api.usegossip.com',
|
|
306
|
+
baseUrl: 'https://api.usegossip.com', // optional; if omitted, env/default is used
|
|
292
307
|
timeout: 10000,
|
|
293
308
|
retryAttempts: 3,
|
|
294
309
|
},
|
|
@@ -296,16 +311,23 @@ await sdk.init({
|
|
|
296
311
|
enabled: false,
|
|
297
312
|
messagesIntervalMs: 5000,
|
|
298
313
|
announcementsIntervalMs: 10000,
|
|
299
|
-
sessionRefreshIntervalMs:
|
|
314
|
+
sessionRefreshIntervalMs: 10000,
|
|
300
315
|
},
|
|
301
316
|
messages: {
|
|
302
317
|
fetchDelayMs: 100,
|
|
303
318
|
maxFetchIterations: 30,
|
|
304
319
|
deduplicationWindowMs: 30000,
|
|
320
|
+
retryDelayMs: 5000,
|
|
305
321
|
},
|
|
306
322
|
announcements: {
|
|
307
323
|
fetchLimit: 500,
|
|
308
|
-
brokenThresholdMs:
|
|
324
|
+
brokenThresholdMs: 60 * 60 * 1000,
|
|
325
|
+
retryDelayMs: 15000,
|
|
326
|
+
},
|
|
327
|
+
sessionRecovery: {
|
|
328
|
+
killedRetryDelayMs: 15 * 60 * 1000,
|
|
329
|
+
JitterMs: 2 * 60 * 1000,
|
|
330
|
+
saturatedRetryDelayMs: 5 * 60 * 1000,
|
|
309
331
|
},
|
|
310
332
|
},
|
|
311
333
|
});
|
|
@@ -330,9 +352,9 @@ await sdk.openSession({
|
|
|
330
352
|
The SDK automatically handles session recovery:
|
|
331
353
|
|
|
332
354
|
1. **Session Lost** - When a session is killed/lost, messages are queued as `WAITING_SESSION`
|
|
333
|
-
2. **Auto-Renewal** -
|
|
334
|
-
3. **Auto-Accept** - When peer sends announcement, SDK
|
|
335
|
-
4. **Message Processing** - After session becomes active, queued messages are sent automatically
|
|
355
|
+
2. **Auto-Renewal** - `updateState()` (called automatically when polling is enabled) re-establishes sessions as needed
|
|
356
|
+
3. **Auto-Accept** - When a peer sends an announcement, the SDK can automatically accept for existing contacts
|
|
357
|
+
4. **Message Processing** - After the session becomes active, queued messages are sent automatically
|
|
336
358
|
|
|
337
359
|
## Types
|
|
338
360
|
|
package/dist/services/message.js
CHANGED
|
@@ -261,7 +261,7 @@ export class MessageService {
|
|
|
261
261
|
whenToSend: message.whenToSend,
|
|
262
262
|
});
|
|
263
263
|
const discussion = await this.queries.discussions.getByOwnerAndContact(message.ownerUserId, message.contactUserId);
|
|
264
|
-
if (discussion) {
|
|
264
|
+
if (discussion && message.type !== MessageType.KEEP_ALIVE) {
|
|
265
265
|
await this.queries.discussions.updateById(discussion.id, {
|
|
266
266
|
lastMessageId: messageId,
|
|
267
267
|
lastMessageContent: message.content,
|
package/package.json
CHANGED