wexa-chat 0.1.0
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 +210 -0
- package/dist/index.d.mts +529 -0
- package/dist/index.d.ts +529 -0
- package/dist/index.js +994 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +964 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
- package/src/config/env.ts +40 -0
- package/src/index.ts +140 -0
- package/src/models/Conversation.model.ts +87 -0
- package/src/models/Message.model.ts +100 -0
- package/src/models/connection.ts +31 -0
- package/src/models/index.ts +4 -0
- package/src/models/indexes.ts +18 -0
- package/src/services/conversations.service.ts +100 -0
- package/src/services/index.ts +3 -0
- package/src/services/messages.service.ts +167 -0
- package/src/services/search/index.ts +2 -0
- package/src/services/search/search.conversations.ts +122 -0
- package/src/services/search/search.messages.ts +125 -0
- package/src/transport/index.ts +63 -0
- package/src/transport/localSubs.ts +102 -0
- package/src/transport/redis.ts +129 -0
- package/src/transport/socket.ts +143 -0
- package/src/types/actor.ts +5 -0
- package/src/types/dto.ts +53 -0
- package/src/types/events.ts +8 -0
- package/src/types/index.ts +8 -0
- package/src/types/init.ts +57 -0
- package/src/utils/ids.ts +55 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/pagination.ts +75 -0
- package/src/utils/validators.ts +146 -0
package/README.md
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# @yourorg/chat-core
|
|
2
|
+
|
|
3
|
+
A TypeScript library for building real-time chat functionality with MongoDB and optional Redis support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Real-time messaging with Redis pub/sub
|
|
8
|
+
- MongoDB-backed conversation and message storage
|
|
9
|
+
- Socket.IO integration
|
|
10
|
+
- Thread support
|
|
11
|
+
- Read receipts
|
|
12
|
+
- Typing indicators
|
|
13
|
+
- Presence tracking
|
|
14
|
+
- Text search capabilities
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @yourorg/chat-core mongoose ioredis socket.io
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import mongoose from 'mongoose';
|
|
26
|
+
import { createServer } from 'http';
|
|
27
|
+
import { initChat } from '@yourorg/chat-core';
|
|
28
|
+
|
|
29
|
+
async function setup() {
|
|
30
|
+
// Create HTTP server (for Socket.IO)
|
|
31
|
+
const server = createServer();
|
|
32
|
+
|
|
33
|
+
// Initialize chat services
|
|
34
|
+
const chat = await initChat(mongoose, {
|
|
35
|
+
// Required
|
|
36
|
+
participantModels: ['User'],
|
|
37
|
+
memberRoles: ['member'],
|
|
38
|
+
mongoUri: 'mongodb://localhost:27017/your-database',
|
|
39
|
+
|
|
40
|
+
// Redis Configuration (optional)
|
|
41
|
+
redis: {
|
|
42
|
+
// Option 1: Use URL
|
|
43
|
+
url: 'redis://localhost:6379',
|
|
44
|
+
|
|
45
|
+
// Option 2: Use individual settings (used if url is not set)
|
|
46
|
+
host: 'localhost',
|
|
47
|
+
port: 6379,
|
|
48
|
+
password: 'optional-password',
|
|
49
|
+
db: 0,
|
|
50
|
+
socketConnectTimeout: 10000,
|
|
51
|
+
keepAlive: 5000
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
// Socket.IO Configuration (optional)
|
|
55
|
+
socket: {
|
|
56
|
+
path: '/socket.io',
|
|
57
|
+
cors: {
|
|
58
|
+
origin: 'http://localhost:3000',
|
|
59
|
+
methods: ['GET', 'POST']
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
// Optional Features
|
|
64
|
+
enableTextSearch: true,
|
|
65
|
+
presence: {
|
|
66
|
+
enabled: true,
|
|
67
|
+
heartbeatSec: 30
|
|
68
|
+
},
|
|
69
|
+
rateLimit: {
|
|
70
|
+
sendPerMin: 60,
|
|
71
|
+
typingPerMin: 10
|
|
72
|
+
}
|
|
73
|
+
}, server); // Pass the HTTP server for Socket.IO
|
|
74
|
+
|
|
75
|
+
// Start the server
|
|
76
|
+
server.listen(3000);
|
|
77
|
+
|
|
78
|
+
return chat;
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Configuration
|
|
83
|
+
|
|
84
|
+
### Required Options
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
{
|
|
88
|
+
// Models that can participate in conversations (e.g., ['User', 'Bot'])
|
|
89
|
+
participantModels: string[];
|
|
90
|
+
|
|
91
|
+
// Valid roles for conversation participants (e.g., ['member', 'admin'])
|
|
92
|
+
memberRoles: string[];
|
|
93
|
+
|
|
94
|
+
// MongoDB connection URI
|
|
95
|
+
mongoUri: string;
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Redis Configuration (Optional)
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
{
|
|
103
|
+
redis?: {
|
|
104
|
+
// Option 1: Use URL
|
|
105
|
+
url?: string;
|
|
106
|
+
|
|
107
|
+
// Option 2: Use individual settings
|
|
108
|
+
host?: string;
|
|
109
|
+
port?: number;
|
|
110
|
+
password?: string;
|
|
111
|
+
db?: number;
|
|
112
|
+
socketConnectTimeout?: number;
|
|
113
|
+
keepAlive?: number;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Socket.IO Configuration (Optional)
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
{
|
|
122
|
+
socket?: {
|
|
123
|
+
path?: string;
|
|
124
|
+
cors?: {
|
|
125
|
+
origin?: string | string[];
|
|
126
|
+
methods?: string[];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Services
|
|
133
|
+
|
|
134
|
+
### Conversations
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// Create or find conversation
|
|
138
|
+
const conversation = await services.conversations.createOrFindConversation({
|
|
139
|
+
organizationId: string;
|
|
140
|
+
a: { model: string; id: string };
|
|
141
|
+
b: { model: string; id: string };
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Messages
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
// Send message
|
|
149
|
+
const message = await services.messages.sendMessage({
|
|
150
|
+
organizationId: string;
|
|
151
|
+
conversationId: string;
|
|
152
|
+
senderModel: string;
|
|
153
|
+
senderId: string;
|
|
154
|
+
text: string;
|
|
155
|
+
kind?: 'text' | 'system';
|
|
156
|
+
parentMessageId?: string; // For threading
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// List messages
|
|
160
|
+
const { items, nextCursor } = await services.messages.listMessages({
|
|
161
|
+
organizationId: string;
|
|
162
|
+
conversationId: string;
|
|
163
|
+
limit?: number;
|
|
164
|
+
cursor?: string;
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Client Integration
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { io } from 'socket.io-client';
|
|
172
|
+
|
|
173
|
+
const socket = io('http://localhost:3000', {
|
|
174
|
+
path: '/socket.io',
|
|
175
|
+
auth: {
|
|
176
|
+
userId: 'user-123',
|
|
177
|
+
userModel: 'User'
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Join a conversation
|
|
182
|
+
socket.emit('join-conversation', conversationId);
|
|
183
|
+
|
|
184
|
+
// Listen for events
|
|
185
|
+
socket.on('chat-event', (event) => {
|
|
186
|
+
switch (event.type) {
|
|
187
|
+
case 'message:created':
|
|
188
|
+
console.log('New message:', event.message);
|
|
189
|
+
break;
|
|
190
|
+
case 'typing':
|
|
191
|
+
console.log('Typing status:', event.state);
|
|
192
|
+
break;
|
|
193
|
+
// ... handle other events
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Send typing indicator
|
|
198
|
+
socket.emit('typing', {
|
|
199
|
+
conversationId: 'conv-123',
|
|
200
|
+
isTyping: true
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Example Project
|
|
205
|
+
|
|
206
|
+
Check out our [demo project](./chat-demo) for a complete example using Next.js.
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
|
|
210
|
+
MIT
|