chatly-sdk 0.0.5 ā 0.0.6
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/CONTRIBUTING.md +658 -0
- package/IMPROVEMENTS.md +402 -0
- package/README.md +1538 -164
- package/dist/index.d.ts +430 -9
- package/dist/index.js +1420 -63
- package/examples/01-basic-chat/README.md +61 -0
- package/examples/01-basic-chat/index.js +58 -0
- package/examples/01-basic-chat/package.json +13 -0
- package/examples/02-group-chat/README.md +78 -0
- package/examples/02-group-chat/index.js +76 -0
- package/examples/02-group-chat/package.json +13 -0
- package/examples/03-offline-messaging/README.md +73 -0
- package/examples/03-offline-messaging/index.js +80 -0
- package/examples/03-offline-messaging/package.json +13 -0
- package/examples/04-live-chat/README.md +80 -0
- package/examples/04-live-chat/index.js +114 -0
- package/examples/04-live-chat/package.json +13 -0
- package/examples/05-hybrid-messaging/README.md +71 -0
- package/examples/05-hybrid-messaging/index.js +106 -0
- package/examples/05-hybrid-messaging/package.json +13 -0
- package/examples/06-postgresql-integration/README.md +101 -0
- package/examples/06-postgresql-integration/adapters/groupStore.js +73 -0
- package/examples/06-postgresql-integration/adapters/messageStore.js +47 -0
- package/examples/06-postgresql-integration/adapters/userStore.js +40 -0
- package/examples/06-postgresql-integration/index.js +92 -0
- package/examples/06-postgresql-integration/package.json +14 -0
- package/examples/06-postgresql-integration/schema.sql +58 -0
- package/examples/08-customer-support/README.md +70 -0
- package/examples/08-customer-support/index.js +104 -0
- package/examples/08-customer-support/package.json +13 -0
- package/examples/README.md +105 -0
- package/jest.config.cjs +28 -0
- package/package.json +12 -8
- package/src/chat/ChatSession.ts +81 -0
- package/src/chat/GroupSession.ts +79 -0
- package/src/constants.ts +61 -0
- package/src/crypto/e2e.ts +0 -20
- package/src/index.ts +525 -63
- package/src/models/mediaTypes.ts +58 -0
- package/src/models/message.ts +4 -1
- package/src/transport/adapters.ts +51 -1
- package/src/transport/memoryTransport.ts +75 -13
- package/src/transport/websocketClient.ts +269 -21
- package/src/transport/websocketServer.ts +26 -26
- package/src/utils/errors.ts +97 -0
- package/src/utils/logger.ts +96 -0
- package/src/utils/mediaUtils.ts +235 -0
- package/src/utils/messageQueue.ts +162 -0
- package/src/utils/validation.ts +99 -0
- package/test/crypto.test.ts +122 -35
- package/test/sdk.test.ts +276 -0
- package/test/validation.test.ts +64 -0
- package/tsconfig.json +11 -10
- package/tsconfig.test.json +11 -0
- package/src/ChatManager.ts +0 -103
- package/src/crypto/keyManager.ts +0 -28
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Basic Chat Example
|
|
2
|
+
|
|
3
|
+
A simple 1:1 chat example using in-memory storage.
|
|
4
|
+
|
|
5
|
+
## What You'll Learn
|
|
6
|
+
|
|
7
|
+
- Creating users
|
|
8
|
+
- Starting a chat session
|
|
9
|
+
- Sending and receiving encrypted messages
|
|
10
|
+
- Decrypting messages
|
|
11
|
+
|
|
12
|
+
## Running the Example
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install
|
|
16
|
+
npm start
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Code Walkthrough
|
|
20
|
+
|
|
21
|
+
This example demonstrates the core SDK functionality:
|
|
22
|
+
|
|
23
|
+
1. **Initialize SDK** with in-memory stores
|
|
24
|
+
2. **Create two users** (Alice and Bob)
|
|
25
|
+
3. **Start a chat session** between them
|
|
26
|
+
4. **Send encrypted messages** from Alice to Bob
|
|
27
|
+
5. **Decrypt and read messages** as Bob
|
|
28
|
+
|
|
29
|
+
All messages are end-to-end encrypted using ECDH + AES-256-GCM.
|
|
30
|
+
|
|
31
|
+
## Output
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
š Chatly SDK - Basic Chat Example
|
|
35
|
+
==================================
|
|
36
|
+
|
|
37
|
+
Creating users...
|
|
38
|
+
ā
Created user: alice (ID: xxx)
|
|
39
|
+
ā
Created user: bob (ID: xxx)
|
|
40
|
+
|
|
41
|
+
Starting chat session...
|
|
42
|
+
ā
Session created between alice and bob
|
|
43
|
+
|
|
44
|
+
Alice sends messages...
|
|
45
|
+
š¤ Alice: Hello Bob!
|
|
46
|
+
š¤ Alice: How are you today?
|
|
47
|
+
š¤ Alice: This is end-to-end encrypted!
|
|
48
|
+
|
|
49
|
+
Bob receives and decrypts messages...
|
|
50
|
+
šØ Bob received: Hello Bob!
|
|
51
|
+
šØ Bob received: How are you today?
|
|
52
|
+
šØ Bob received: This is end-to-end encrypted!
|
|
53
|
+
|
|
54
|
+
ā
All messages encrypted and decrypted successfully!
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Key Concepts
|
|
58
|
+
|
|
59
|
+
- **In-Memory Storage**: No database required for testing
|
|
60
|
+
- **E2E Encryption**: Messages encrypted on sender, decrypted on receiver
|
|
61
|
+
- **Session Management**: Each conversation has a unique session
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ChatSDK, InMemoryUserStore, InMemoryMessageStore, InMemoryGroupStore, LogLevel } from 'chatly-sdk';
|
|
2
|
+
|
|
3
|
+
async function main() {
|
|
4
|
+
console.log('š Chatly SDK - Basic Chat Example');
|
|
5
|
+
console.log('==================================\n');
|
|
6
|
+
|
|
7
|
+
// Initialize SDK with in-memory storage
|
|
8
|
+
const sdk = new ChatSDK({
|
|
9
|
+
userStore: new InMemoryUserStore(),
|
|
10
|
+
messageStore: new InMemoryMessageStore(),
|
|
11
|
+
groupStore: new InMemoryGroupStore(),
|
|
12
|
+
logLevel: LogLevel.NONE, // Disable logs for cleaner output
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Step 1: Create users
|
|
16
|
+
console.log('Creating users...');
|
|
17
|
+
const alice = await sdk.createUser('alice');
|
|
18
|
+
const bob = await sdk.createUser('bob');
|
|
19
|
+
console.log(`ā
Created user: ${alice.username} (ID: ${alice.id.substring(0, 8)}...)`);
|
|
20
|
+
console.log(`ā
Created user: ${bob.username} (ID: ${bob.id.substring(0, 8)}...)\n`);
|
|
21
|
+
|
|
22
|
+
// Step 2: Start a chat session
|
|
23
|
+
console.log('Starting chat session...');
|
|
24
|
+
const session = await sdk.startSession(alice, bob);
|
|
25
|
+
console.log(`ā
Session created between ${alice.username} and ${bob.username}\n`);
|
|
26
|
+
|
|
27
|
+
// Step 3: Alice sends messages
|
|
28
|
+
console.log('Alice sends messages...');
|
|
29
|
+
sdk.setCurrentUser(alice);
|
|
30
|
+
|
|
31
|
+
const messages = [
|
|
32
|
+
'Hello Bob!',
|
|
33
|
+
'How are you today?',
|
|
34
|
+
'This is end-to-end encrypted!',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
for (const text of messages) {
|
|
38
|
+
await sdk.sendMessage(session, text);
|
|
39
|
+
console.log(`š¤ Alice: ${text}`);
|
|
40
|
+
}
|
|
41
|
+
console.log();
|
|
42
|
+
|
|
43
|
+
// Step 4: Bob receives and decrypts messages
|
|
44
|
+
console.log('Bob receives and decrypts messages...');
|
|
45
|
+
sdk.setCurrentUser(bob);
|
|
46
|
+
|
|
47
|
+
const receivedMessages = await sdk.getMessagesForUser(bob.id);
|
|
48
|
+
|
|
49
|
+
for (const msg of receivedMessages) {
|
|
50
|
+
const plaintext = await sdk.decryptMessage(msg, bob);
|
|
51
|
+
console.log(`šØ Bob received: ${plaintext}`);
|
|
52
|
+
}
|
|
53
|
+
console.log();
|
|
54
|
+
|
|
55
|
+
console.log('ā
All messages encrypted and decrypted successfully!');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chatly-sdk-basic-chat-example",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Basic 1:1 chat example using Chatly SDK",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node index.js"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"chatly-sdk": "^0.0.5"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Group Chat Example
|
|
2
|
+
|
|
3
|
+
Multi-user encrypted group messaging.
|
|
4
|
+
|
|
5
|
+
## What You'll Learn
|
|
6
|
+
|
|
7
|
+
- Creating groups
|
|
8
|
+
- Adding multiple members
|
|
9
|
+
- Sending messages to groups
|
|
10
|
+
- Group encryption
|
|
11
|
+
- Retrieving group messages
|
|
12
|
+
|
|
13
|
+
## Running the Example
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install
|
|
17
|
+
npm start
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## How It Works
|
|
21
|
+
|
|
22
|
+
This example demonstrates **group chat**:
|
|
23
|
+
- Create a group with multiple members
|
|
24
|
+
- All messages encrypted with group key
|
|
25
|
+
- All members can send and receive
|
|
26
|
+
- End-to-end encrypted group messaging
|
|
27
|
+
|
|
28
|
+
## Output
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
š„ Chatly SDK - Group Chat Example
|
|
32
|
+
==================================
|
|
33
|
+
|
|
34
|
+
Creating users...
|
|
35
|
+
ā
Alice created
|
|
36
|
+
ā
Bob created
|
|
37
|
+
ā
Charlie created
|
|
38
|
+
|
|
39
|
+
Creating group...
|
|
40
|
+
ā
Group created: Team Chat
|
|
41
|
+
ā
Members: 3
|
|
42
|
+
|
|
43
|
+
Alice sends to group...
|
|
44
|
+
š¤ Alice: Hello team!
|
|
45
|
+
š¤ Alice: Let's discuss the project
|
|
46
|
+
|
|
47
|
+
Bob sends to group...
|
|
48
|
+
š¤ Bob: Sounds good!
|
|
49
|
+
|
|
50
|
+
Charlie sends to group...
|
|
51
|
+
š¤ Charlie: I'm in!
|
|
52
|
+
|
|
53
|
+
All members receive messages...
|
|
54
|
+
šØ Bob received 4 messages
|
|
55
|
+
šØ Charlie received 4 messages
|
|
56
|
+
|
|
57
|
+
Decrypting messages...
|
|
58
|
+
š¬ Alice: Hello team!
|
|
59
|
+
š¬ Alice: Let's discuss the project
|
|
60
|
+
š¬ Bob: Sounds good!
|
|
61
|
+
š¬ Charlie: I'm in!
|
|
62
|
+
|
|
63
|
+
ā
Group chat works perfectly!
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Key Features
|
|
67
|
+
|
|
68
|
+
- **Multi-user**: 2-256 members per group
|
|
69
|
+
- **Encrypted**: End-to-end encryption for all messages
|
|
70
|
+
- **Scalable**: Same encryption performance regardless of group size
|
|
71
|
+
- **Persistent**: Messages stored in database
|
|
72
|
+
|
|
73
|
+
## Use Cases
|
|
74
|
+
|
|
75
|
+
- **Team Chat** (Slack, Microsoft Teams)
|
|
76
|
+
- **Family Groups** (WhatsApp groups)
|
|
77
|
+
- **Community Channels**
|
|
78
|
+
- **Project Collaboration**
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ChatSDK, InMemoryUserStore, InMemoryMessageStore, InMemoryGroupStore, LogLevel } from 'chatly-sdk';
|
|
2
|
+
|
|
3
|
+
async function main() {
|
|
4
|
+
console.log('š„ Chatly SDK - Group Chat Example');
|
|
5
|
+
console.log('==================================\n');
|
|
6
|
+
|
|
7
|
+
// Initialize SDK
|
|
8
|
+
const sdk = new ChatSDK({
|
|
9
|
+
userStore: new InMemoryUserStore(),
|
|
10
|
+
messageStore: new InMemoryMessageStore(),
|
|
11
|
+
groupStore: new InMemoryGroupStore(),
|
|
12
|
+
logLevel: LogLevel.NONE,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
// Create users
|
|
16
|
+
console.log('Creating users...');
|
|
17
|
+
const alice = await sdk.createUser('alice');
|
|
18
|
+
const bob = await sdk.createUser('bob');
|
|
19
|
+
const charlie = await sdk.createUser('charlie');
|
|
20
|
+
console.log('ā
Alice created');
|
|
21
|
+
console.log('ā
Bob created');
|
|
22
|
+
console.log('ā
Charlie created\n');
|
|
23
|
+
|
|
24
|
+
// Create group
|
|
25
|
+
console.log('Creating group...');
|
|
26
|
+
const group = await sdk.createGroup('Team Chat', [alice, bob, charlie]);
|
|
27
|
+
console.log(`ā
Group created: ${group.group.name}`);
|
|
28
|
+
console.log(`ā
Members: ${group.group.members.length}\n`);
|
|
29
|
+
|
|
30
|
+
// Alice sends messages to group
|
|
31
|
+
console.log('Alice sends to group...');
|
|
32
|
+
sdk.setCurrentUser(alice);
|
|
33
|
+
await sdk.sendMessage(group, 'Hello team!');
|
|
34
|
+
console.log('š¤ Alice: Hello team!');
|
|
35
|
+
|
|
36
|
+
await sdk.sendMessage(group, 'Let\'s discuss the project');
|
|
37
|
+
console.log('š¤ Alice: Let\'s discuss the project\n');
|
|
38
|
+
|
|
39
|
+
// Bob sends to group
|
|
40
|
+
console.log('Bob sends to group...');
|
|
41
|
+
sdk.setCurrentUser(bob);
|
|
42
|
+
await sdk.sendMessage(group, 'Sounds good!');
|
|
43
|
+
console.log('š¤ Bob: Sounds good!\n');
|
|
44
|
+
|
|
45
|
+
// Charlie sends to group
|
|
46
|
+
console.log('Charlie sends to group...');
|
|
47
|
+
sdk.setCurrentUser(charlie);
|
|
48
|
+
await sdk.sendMessage(group, 'I\'m in!');
|
|
49
|
+
console.log('š¤ Charlie: I\'m in!\n');
|
|
50
|
+
|
|
51
|
+
// All members receive messages
|
|
52
|
+
console.log('All members receive messages...');
|
|
53
|
+
const bobMessages = await sdk.getMessagesForGroup(group.group.id);
|
|
54
|
+
const charlieMessages = await sdk.getMessagesForGroup(group.group.id);
|
|
55
|
+
console.log(`šØ Bob received ${bobMessages.length} messages`);
|
|
56
|
+
console.log(`šØ Charlie received ${charlieMessages.length} messages\n`);
|
|
57
|
+
|
|
58
|
+
// Decrypt and display messages
|
|
59
|
+
console.log('Decrypting messages...');
|
|
60
|
+
for (const msg of bobMessages) {
|
|
61
|
+
const plaintext = await sdk.decryptMessage(msg, bob);
|
|
62
|
+
const sender = msg.senderId === alice.id ? 'Alice' :
|
|
63
|
+
msg.senderId === bob.id ? 'Bob' : 'Charlie';
|
|
64
|
+
console.log(`š¬ ${sender}: ${plaintext}`);
|
|
65
|
+
}
|
|
66
|
+
console.log();
|
|
67
|
+
|
|
68
|
+
console.log('ā
Group chat works perfectly!');
|
|
69
|
+
console.log('\nš” Key Features:');
|
|
70
|
+
console.log(' - Multi-user (2-256 members)');
|
|
71
|
+
console.log(' - End-to-end encrypted');
|
|
72
|
+
console.log(' - All members can send/receive');
|
|
73
|
+
console.log(' - Messages stored in database');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chatly-sdk-group-chat-example",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Group chat example using Chatly SDK",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node index.js"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"chatly-sdk": "^0.0.5"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Offline Messaging Example
|
|
2
|
+
|
|
3
|
+
Demonstrates asynchronous messaging without WebSocket (like email).
|
|
4
|
+
|
|
5
|
+
## What You'll Learn
|
|
6
|
+
|
|
7
|
+
- Messaging without real-time connection
|
|
8
|
+
- Message persistence in database
|
|
9
|
+
- Offline-first architecture
|
|
10
|
+
- Message retrieval and synchronization
|
|
11
|
+
|
|
12
|
+
## Running the Example
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install
|
|
16
|
+
npm start
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## How It Works
|
|
20
|
+
|
|
21
|
+
This example shows **asynchronous messaging** where:
|
|
22
|
+
- No WebSocket connection required
|
|
23
|
+
- Messages stored in database immediately
|
|
24
|
+
- Users retrieve messages when they check
|
|
25
|
+
- Works like email or traditional messaging
|
|
26
|
+
|
|
27
|
+
## Code Walkthrough
|
|
28
|
+
|
|
29
|
+
1. **No Transport Layer** - SDK initialized without WebSocket
|
|
30
|
+
2. **Messages Stored** - All messages go directly to database
|
|
31
|
+
3. **Pull-Based** - Users fetch messages when ready
|
|
32
|
+
4. **Offline-First** - Works without internet connection
|
|
33
|
+
|
|
34
|
+
## Output
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
š§ Chatly SDK - Offline Messaging Example
|
|
38
|
+
=========================================
|
|
39
|
+
|
|
40
|
+
Creating users...
|
|
41
|
+
ā
Alice created
|
|
42
|
+
ā
Bob created
|
|
43
|
+
|
|
44
|
+
Alice sends messages (offline mode)...
|
|
45
|
+
š¤ Message queued: Hey Bob, check this out later!
|
|
46
|
+
š¤ Message queued: No rush to reply
|
|
47
|
+
š¤ Message queued: This works like email
|
|
48
|
+
|
|
49
|
+
Bob checks messages later...
|
|
50
|
+
š¬ Bob has 3 new messages
|
|
51
|
+
šØ Message 1: Hey Bob, check this out later!
|
|
52
|
+
šØ Message 2: No rush to reply
|
|
53
|
+
šØ Message 3: This works like email
|
|
54
|
+
|
|
55
|
+
ā
Offline messaging works perfectly!
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Use Cases
|
|
59
|
+
|
|
60
|
+
- **Email-like systems**
|
|
61
|
+
- **Notification systems**
|
|
62
|
+
- **Asynchronous team communication**
|
|
63
|
+
- **IoT device messaging**
|
|
64
|
+
- **Store-and-forward systems**
|
|
65
|
+
|
|
66
|
+
## Key Difference from Live Chat
|
|
67
|
+
|
|
68
|
+
| Feature | Offline Messaging | Live Chat |
|
|
69
|
+
|---------|------------------|-----------|
|
|
70
|
+
| Transport | None | WebSocket |
|
|
71
|
+
| Delivery | Database | Real-time |
|
|
72
|
+
| Retrieval | Pull (fetch) | Push (events) |
|
|
73
|
+
| Use Case | Email-like | WhatsApp-like |
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { ChatSDK, InMemoryUserStore, InMemoryMessageStore, InMemoryGroupStore, LogLevel } from 'chatly-sdk';
|
|
2
|
+
|
|
3
|
+
async function main() {
|
|
4
|
+
console.log('š§ Chatly SDK - Offline Messaging Example');
|
|
5
|
+
console.log('=========================================\n');
|
|
6
|
+
|
|
7
|
+
// Initialize SDK WITHOUT transport (offline mode)
|
|
8
|
+
const sdk = new ChatSDK({
|
|
9
|
+
userStore: new InMemoryUserStore(),
|
|
10
|
+
messageStore: new InMemoryMessageStore(),
|
|
11
|
+
groupStore: new InMemoryGroupStore(),
|
|
12
|
+
// No transport = offline/asynchronous messaging
|
|
13
|
+
logLevel: LogLevel.NONE,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// Create users
|
|
17
|
+
console.log('Creating users...');
|
|
18
|
+
const alice = await sdk.createUser('alice');
|
|
19
|
+
const bob = await sdk.createUser('bob');
|
|
20
|
+
console.log('ā
Alice created');
|
|
21
|
+
console.log('ā
Bob created\n');
|
|
22
|
+
|
|
23
|
+
// Start session
|
|
24
|
+
const session = await sdk.startSession(alice, bob);
|
|
25
|
+
|
|
26
|
+
// Alice sends messages (offline mode)
|
|
27
|
+
console.log('Alice sends messages (offline mode)...');
|
|
28
|
+
sdk.setCurrentUser(alice);
|
|
29
|
+
|
|
30
|
+
const messages = [
|
|
31
|
+
'Hey Bob, check this out later!',
|
|
32
|
+
'No rush to reply',
|
|
33
|
+
'This works like email',
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
for (const text of messages) {
|
|
37
|
+
await sdk.sendMessage(session, text);
|
|
38
|
+
console.log(`š¤ Message queued: ${text}`);
|
|
39
|
+
}
|
|
40
|
+
console.log();
|
|
41
|
+
|
|
42
|
+
// Simulate time passing...
|
|
43
|
+
console.log('ā° Time passes... Bob checks messages later...\n');
|
|
44
|
+
|
|
45
|
+
// Bob retrieves messages
|
|
46
|
+
console.log('Bob checks messages later...');
|
|
47
|
+
sdk.setCurrentUser(bob);
|
|
48
|
+
|
|
49
|
+
const receivedMessages = await sdk.getMessagesForUser(bob.id);
|
|
50
|
+
console.log(`š¬ Bob has ${receivedMessages.length} new messages`);
|
|
51
|
+
|
|
52
|
+
for (let i = 0; i < receivedMessages.length; i++) {
|
|
53
|
+
const plaintext = await sdk.decryptMessage(receivedMessages[i], bob);
|
|
54
|
+
console.log(`šØ Message ${i + 1}: ${plaintext}`);
|
|
55
|
+
}
|
|
56
|
+
console.log();
|
|
57
|
+
|
|
58
|
+
// Bob replies (also offline)
|
|
59
|
+
console.log('Bob replies (also offline)...');
|
|
60
|
+
await sdk.sendMessage(session, 'Thanks Alice, got your messages!');
|
|
61
|
+
console.log('š¤ Bob: Thanks Alice, got your messages!\n');
|
|
62
|
+
|
|
63
|
+
// Alice checks later
|
|
64
|
+
console.log('Alice checks messages later...');
|
|
65
|
+
sdk.setCurrentUser(alice);
|
|
66
|
+
|
|
67
|
+
const aliceMessages = await sdk.getMessagesForUser(alice.id);
|
|
68
|
+
const bobReply = aliceMessages[aliceMessages.length - 1];
|
|
69
|
+
const replyText = await sdk.decryptMessage(bobReply, alice);
|
|
70
|
+
console.log(`šØ Alice received: ${replyText}\n`);
|
|
71
|
+
|
|
72
|
+
console.log('ā
Offline messaging works perfectly!');
|
|
73
|
+
console.log('\nš” Key Points:');
|
|
74
|
+
console.log(' - No WebSocket connection needed');
|
|
75
|
+
console.log(' - Messages stored in database');
|
|
76
|
+
console.log(' - Users fetch messages when ready');
|
|
77
|
+
console.log(' - Works like email or async messaging');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chatly-sdk-offline-messaging-example",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Offline/asynchronous messaging example using Chatly SDK",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node index.js"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"chatly-sdk": "^0.0.5"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Live Chat Example
|
|
2
|
+
|
|
3
|
+
Real-time chat using WebSocket (WhatsApp-style).
|
|
4
|
+
|
|
5
|
+
## What You'll Learn
|
|
6
|
+
|
|
7
|
+
- Real-time messaging with WebSocket
|
|
8
|
+
- Event-driven architecture
|
|
9
|
+
- Connection state management
|
|
10
|
+
- Instant message delivery
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
You need a WebSocket server running. See [`../10-websocket-server/`](../10-websocket-server/) for a simple server implementation.
|
|
15
|
+
|
|
16
|
+
## Running the Example
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# Terminal 1: Start WebSocket server
|
|
20
|
+
cd ../10-websocket-server
|
|
21
|
+
npm install
|
|
22
|
+
npm start
|
|
23
|
+
|
|
24
|
+
# Terminal 2: Run this example
|
|
25
|
+
cd ../04-live-chat
|
|
26
|
+
npm install
|
|
27
|
+
npm start
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## How It Works
|
|
31
|
+
|
|
32
|
+
This example demonstrates **real-time messaging**:
|
|
33
|
+
- WebSocket connection for instant delivery
|
|
34
|
+
- Events for real-time UI updates
|
|
35
|
+
- Automatic reconnection on disconnect
|
|
36
|
+
- Message queue for offline scenarios
|
|
37
|
+
|
|
38
|
+
## Output
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
š¬ Chatly SDK - Live Chat Example
|
|
42
|
+
=================================
|
|
43
|
+
|
|
44
|
+
Connecting to WebSocket server...
|
|
45
|
+
š¢ Connected to wss://localhost:8080
|
|
46
|
+
|
|
47
|
+
Creating users...
|
|
48
|
+
ā
Alice connected
|
|
49
|
+
ā
Bob connected
|
|
50
|
+
|
|
51
|
+
Setting up event listeners...
|
|
52
|
+
ā
Event listeners ready
|
|
53
|
+
|
|
54
|
+
Alice sends real-time messages...
|
|
55
|
+
š¤ Alice: Hi Bob! (sent instantly)
|
|
56
|
+
šØ Bob received instantly: Hi Bob!
|
|
57
|
+
|
|
58
|
+
š¤ Alice: How's it going?
|
|
59
|
+
šØ Bob received instantly: How's it going?
|
|
60
|
+
|
|
61
|
+
š¤ Alice: This is real-time!
|
|
62
|
+
šØ Bob received instantly: This is real-time!
|
|
63
|
+
|
|
64
|
+
ā
All messages delivered in real-time!
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Key Features
|
|
68
|
+
|
|
69
|
+
- **Instant Delivery**: Messages delivered immediately via WebSocket
|
|
70
|
+
- **Event-Driven**: Real-time events for UI updates
|
|
71
|
+
- **Auto-Reconnect**: Automatically reconnects if connection drops
|
|
72
|
+
- **Message Queue**: Queues messages if offline, sends when reconnected
|
|
73
|
+
|
|
74
|
+
## Use Cases
|
|
75
|
+
|
|
76
|
+
- **Messaging Apps** (WhatsApp, Telegram)
|
|
77
|
+
- **Live Customer Support**
|
|
78
|
+
- **Team Collaboration** (Slack, Discord)
|
|
79
|
+
- **Gaming Chat**
|
|
80
|
+
- **Social Media DMs**
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ChatSDK,
|
|
3
|
+
InMemoryUserStore,
|
|
4
|
+
InMemoryMessageStore,
|
|
5
|
+
InMemoryGroupStore,
|
|
6
|
+
WebSocketClient,
|
|
7
|
+
EVENTS,
|
|
8
|
+
ConnectionState,
|
|
9
|
+
LogLevel
|
|
10
|
+
} from 'chatly-sdk';
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
console.log('š¬ Chatly SDK - Live Chat Example');
|
|
14
|
+
console.log('=================================\n');
|
|
15
|
+
|
|
16
|
+
// Create WebSocket transport for real-time communication
|
|
17
|
+
console.log('Connecting to WebSocket server...');
|
|
18
|
+
const transport = new WebSocketClient('ws://localhost:8080');
|
|
19
|
+
|
|
20
|
+
// Initialize SDK with WebSocket transport
|
|
21
|
+
const sdk = new ChatSDK({
|
|
22
|
+
userStore: new InMemoryUserStore(),
|
|
23
|
+
messageStore: new InMemoryMessageStore(),
|
|
24
|
+
groupStore: new InMemoryGroupStore(),
|
|
25
|
+
transport, // Add WebSocket for real-time
|
|
26
|
+
logLevel: LogLevel.NONE,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Listen for connection state changes
|
|
30
|
+
sdk.on(EVENTS.CONNECTION_STATE_CHANGED, (state) => {
|
|
31
|
+
switch (state) {
|
|
32
|
+
case ConnectionState.CONNECTED:
|
|
33
|
+
console.log('š¢ Connected to WebSocket server');
|
|
34
|
+
break;
|
|
35
|
+
case ConnectionState.CONNECTING:
|
|
36
|
+
console.log('š” Connecting...');
|
|
37
|
+
break;
|
|
38
|
+
case ConnectionState.DISCONNECTED:
|
|
39
|
+
console.log('š“ Disconnected');
|
|
40
|
+
break;
|
|
41
|
+
case ConnectionState.RECONNECTING:
|
|
42
|
+
console.log('š” Reconnecting...');
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Create users
|
|
48
|
+
console.log('\nCreating users...');
|
|
49
|
+
const alice = await sdk.createUser('alice');
|
|
50
|
+
const bob = await sdk.createUser('bob');
|
|
51
|
+
console.log('ā
Alice created');
|
|
52
|
+
console.log('ā
Bob created\n');
|
|
53
|
+
|
|
54
|
+
// Start session
|
|
55
|
+
const session = await sdk.startSession(alice, bob);
|
|
56
|
+
|
|
57
|
+
// Set up event listeners for real-time updates
|
|
58
|
+
console.log('Setting up event listeners...');
|
|
59
|
+
|
|
60
|
+
sdk.on(EVENTS.MESSAGE_SENT, (message) => {
|
|
61
|
+
console.log(`ā
Message sent (ID: ${message.id.substring(0, 8)}...)`);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
sdk.on(EVENTS.MESSAGE_RECEIVED, async (message) => {
|
|
65
|
+
const currentUser = sdk.getCurrentUser();
|
|
66
|
+
if (currentUser) {
|
|
67
|
+
const plaintext = await sdk.decryptMessage(message, currentUser);
|
|
68
|
+
console.log(`šØ ${currentUser.username} received instantly: ${plaintext}`);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
sdk.on(EVENTS.MESSAGE_FAILED, (message, error) => {
|
|
73
|
+
console.error(`ā Message failed: ${error.message}`);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
console.log('ā
Event listeners ready\n');
|
|
77
|
+
|
|
78
|
+
// Wait for connection
|
|
79
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
80
|
+
|
|
81
|
+
// Alice sends real-time messages
|
|
82
|
+
console.log('Alice sends real-time messages...');
|
|
83
|
+
sdk.setCurrentUser(alice);
|
|
84
|
+
|
|
85
|
+
const messages = [
|
|
86
|
+
'Hi Bob! (sent instantly)',
|
|
87
|
+
'How\'s it going?',
|
|
88
|
+
'This is real-time!',
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
for (const text of messages) {
|
|
92
|
+
await sdk.sendMessage(session, text);
|
|
93
|
+
console.log(`š¤ Alice: ${text}`);
|
|
94
|
+
await new Promise(resolve => setTimeout(resolve, 500)); // Small delay for demo
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
console.log();
|
|
98
|
+
|
|
99
|
+
// Wait a bit for all messages to be received
|
|
100
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
101
|
+
|
|
102
|
+
console.log('ā
All messages delivered in real-time!');
|
|
103
|
+
console.log('\nš” Key Features:');
|
|
104
|
+
console.log(' - Instant delivery via WebSocket');
|
|
105
|
+
console.log(' - Real-time events for UI updates');
|
|
106
|
+
console.log(' - Automatic reconnection');
|
|
107
|
+
console.log(' - Message queue for offline scenarios');
|
|
108
|
+
|
|
109
|
+
// Disconnect
|
|
110
|
+
await sdk.disconnect();
|
|
111
|
+
console.log('\nš Disconnected from server');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "chatly-sdk-live-chat-example",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Real-time live chat example using Chatly SDK with WebSocket",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node index.js"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"chatly-sdk": "^0.0.5"
|
|
12
|
+
}
|
|
13
|
+
}
|